- Home /
How do i reference a non static singleton?
I haven't been able to find anything useful on this considering I don't know how to look for it, but I was told to make a singleton for my code since I needed a variable that could be used on another script since the variable is being changed. But when I use it on another script it does not produce the same number since it is static. So basically the first code is:
public int itemNumber;
static public classname instance;
void Start()
{
itemNumber = Random.Range (0, 7)
}
And in the other script I try to use whatever number itemNumber generated by doing:
public int itemNumberTwo
SpinWheel.instance.itemNumber = temNumberTwo
But when I run the script it later does not result in the same numbers I'm guessing because it is static. I try to remove that part but then it says an object reference is needed to access the non-static field. Any ideas? Sorry if the way I tried to explain this doesn't make sense don't know a bit of C# mostly just java.
Well my first suggestion is that if someone told you to use a "singleton" when you ask on how yo access the var in another script i would suggest not taking into account that suggestions. For changing the var you just need a reference to that object using getcomponent for example if its a monobehaviour, and leave singletons for managers or try to avoid them. Static means that is associated to the class rather than to an object, if you dont want it to be static you need the object reference.
I don't think a "non-static singleton" is a thing.... Even FindObjectOfType is mostly for laziness. If you need scripts to access eachother, capture a reference in Editor or at Runtime during instantiation, there is no excuse for not having a reference to an object you need a reference to, unless you are using someone else's code and don't have the knowledge to change it. Think about it this way, not mater how pre-defined or procedural something is, YOU still have 100% control of when and where something is created. So capture that if you need it, otherwise you're jumping through hoops for no reason. It takes a marginal amount of resources to pool a list together of References, or a few managed dictionaries even. Remember that ANYTHING that is commonly tied to another object without an actual object reference and can have multiple instance of that, should have an ID or a Unique Identifier that is used to communicate references linking together, its also very simple to write an ID generator that will generate unique ID's every time. This is especially useful when you have a 3D representation of a data object, like an inventory item. You never have to tie the item data to the represented object, you simply give the data object a unique ID and give the 3D object the unique ID for reference purposes. You can then pass methods around by simply using the ID of the objects. For creating Unique ID's for 3D objects without adding additional scripts to the objects, you can simply use Vector3.sqe$$anonymous$$agnitude() to get a unique ID for each object. Unless somehow 2 scene objects share the EXACT same location, which is usually unlikely, but you can create a nearly guaranteed 3d unique ID by compining the position magnitutde with the eulers magnitude, very very unlikely for 2 objects to be in the EXACT same location and same rotation, if there are, it is likely poor procedural instantiation.
Answer by toddisarockstar · Apr 01, 2019 at 04:26 AM
a static variable creates only ONE instance of the variable that is shared by all.
if you want to change it from another script or class you just have to use the class or script name to refer to it.
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public static int MySharedNumber;
void Start () {
//access this from THIS script like this
MySharedNumber = Random.Range (0, 10);
// to access the number from other scripts
// just use the class name that it started from like this
print ("this line works from any script"+example.MySharedNumber);
}
}
Your answer
Follow this Question
Related Questions
How do i reference a non static singleton? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how do I create a static Instance in javascript 2 Answers
How to make a c# variabel equal a static java variabel? 3 Answers