HELP
Development FAQ
How do you use a Singleton?
7min
a singleton is a design pattern that ensures there is only one instance of a particular class within a program the singleton pattern provides a single global instance of a class, making it easily accessible from other scripts that need it, such as managing logic, audio, ui, and centralized resources within the world in zepetoscript, you can implement a singleton in the following way private static instance classname = null; public static get instance() classname { 	if (this instance == null) { 	 this instance = gameobject findobjectoftype\<classname>(); 	} 	return this instance; } 👍 tips if there is already a gameobject with a singleton script attached in the scene, you can use this format if there isn't one, add code to create the gameobject example new gameobject("objectname") addcomponent\<classname>(); let's implement a singleton for managing ui in an example 1\) add ui > text and button to the scene set up the logic to increase the score when the "increasescore" button is pressed set up the logic to decrease the score when the "decreasescore" button is pressed 2\) create a script called "scoremanager," where the singleton will be applied the scoremanager script will be responsible for increasing/decreasing the score and updating the text when called from other scripts import { zepetoscriptbehaviour } from 'zepeto script'; import { gameobject } from 'unityengine'; import { text } from 'unityengine ui'; export default class scoremanager extends zepetoscriptbehaviour { // singleton declaration private static instance scoremanager = null; public static get instance() scoremanager { if (this instance == null) { this instance = gameobject findobjectoftype\<scoremanager>(); } return this instance; } // variable declaration public scoretext text; private currentscore number; start() { // set initial score to 0 for testing this currentscore = 0; } // other scripts can call increasescore public increasescore(amount number) { this currentscore += amount; this scoretext text = `score ${this currentscore}`; } // other scripts can call decreasescore public decreasescore(amount number) { this currentscore = amount; this scoretext text = `score ${this currentscore}`; } } script description private static instance this is a private static variable within the class, initially set to null public static get instance this static property provides a way for external code to access the unique instance of this class check if (this \\ instance == null) if it’s null, use gameobject findobjectoftype<>() to find and return a component of type classname, and assign it to the static variable 3\) after writing the script, return to the inspector and connect the text component 4\) next, create a script called "scorelogicsample" to demonstrate how to call the singleton ensure that scoremanager and scorelogicsample are in the same directory write code like this import { zepetoscriptbehaviour } from 'zepeto script'; import { gameobject } from 'unityengine'; import { text, button } from 'unityengine ui'; // import singleton script import scoremanager from ' /scoremanager'; export default class scorelogicsample extends zepetoscriptbehaviour { public increasescorebutton button; public decreasescorebutton button; // declare variable of type scoremanager private scoremanager scoremanager; start() { // assign scoremanager to the variable this scoremanager = gameobject findobjectoftype\<scoremanager>(); this increasescorebutton onclick addlistener(()=> { // call the function declared in singleton this scoremanager increasescore(10); }); this decreasescorebutton onclick addlistener(()=> { // call the function declared in singleton this scoremanager decreasescore(10); }); } } it's important to specify the path correctly when getting the singleton script you can adapt the part where scoremanager is called in your own script as needed 5\) press the \[▶︎(play)] button to test you should observe the functions in scoremanager being called when you click the buttons in scorelogicsample, making it work as expected