- Home /
Same Prefab, Same Script, Different Values?
I have a two copies of the same prefab, both of which have the same script assigned to themselves. In the script, I have two public int variables set up that I would like to use as unique identifiers for each prefab. However, whenever I change one of the values in the inspector for either object, it changes in the other as well. Is there anyway to make those values unique to each prefab? I have tried setting them to private to see if that would work and no luck.
Here is an example of my script that is attached to the prefab:
public class DroneBay : MonoBehaviour
{
public int bayID;
public int droneID;
}
Any help is appreciated :)
Answer by Kishotta · Jun 27, 2017 at 01:49 AM
That's actually how prefabs are designed to work. They should all be exactly the same when instantiated. What you probably want to do is initialize those values when you instantiate the object. ie:
int bayIdentifier = 0;
int droneIdentifier = 0;
DroneBay newDroneBay = Instantiate (DroneBayPrefab).GetComponent<DroneBay> ();
newDroneBay.bayID = bayIdentifier++; // Or however you want to assign these ids
newDroneBay.droneID = droneIdentifier++;
That would make a lot of sense then, haha. I'll give the code assignment with instantiation a try. Thanks for the info.
Your answer
Follow this Question
Related Questions
PlayFab CloudScript NullReferenceException 0 Answers
Activate seperated dolly tracks 0 Answers
How to tell if a Canvas UI slider is being clicked not over Value 2 Answers
Pong goal collision 3 Answers