I’m trying to instantiate 5 of the same object that store a variable of the order they were created. Like, I would have a variable orderCreated and the first would be one, the next would be 2, etc. I have a script attached to the prefab, and have tried setting orderCreated to a variable in the script that instantiates the object that increases by one very time it creates something, but i can’t access the orderCreated in update because it’s created in awake. update is private btw if that matters.
You can also do this by:
GameObject yourGameObject = Instantiate(yourPrefab, yourTransform, yourRotation);
yourGameObject.GetComponent<yourComponent>().yourVariable = ___;
You can do this by creating a reference for your object, so when you instantiate do it like this:
YourClass prefab;
YourClass yourClass;
Awake(){
yourClass=Instantiate(prefab);
}
Update(){
yourClass.orderCreated=1;
}
Then you can access your new object anytime through the reference created, providing you declare the reference at class level. But this seems like a lot of extra for for just a counter, you should set it when you create the object, you can increment it in Start or Awake and decrement it in OnDestroy in the class of your object, which I’ve been denoting as YourClass, using a static int defined in that class. Making the variable static will make it shared amongst all instances and you can access it without a reference. This will if you want to keep track of how many objects of a type you have, if you want to keep track of different objects together then put the static int on your character instead.