- Home /
Instantiated clones using each other`s script
Hello, I got a problem - I am doing RTS game, where player will not control AI, and I got a script attached to a prefab of my basic unit, which is of course cloning when we produce it. The script is about searching for the closest enemies, and it is working for one unit, but when I produce more units, then every unit is moving towards enemy which was searched by the unit which script was done first, just like there was only one script for every unit, but I got attached it to every single clone (in prefab). I would share a code with you guys but there is a lot of refers to another scripts like moving, attacking etc. Please help.
Can you tell me how did you attach that script to cloned objects?
At run time or already attached in prefab?
If you attached it at runtime, then :
Did you attach that script by using :
clonedGameObject.AddComponent<this>();
or
clonedGameObject.AddComponent<AIScript>();
It is attached to the prefab, and every other scripts too which are refering to it while enemy is found at runtime. By the way I found this code to instantiate clones as child, because i am storing (both enemies and my units) their transforms in parent`s lists to make this script work (enemies and my units have other parent and other list).>
SpawnedUnit = Instantiate (unit, unit.transform.position, unit.transform.rotation) as GameObject;
SpawnedUnit.transform.parent = GameObject.Find ("TemplarSpawnPlace").transform;
SpawnedUnit.name += NameCounter;
NameCounter++;
So I am saying that SpawnedUnit is my Unit but its cloned as child, and then I change it`s name with counter to search for enemies properly(otherwise it will search for every new spawned enemy unit).
Your answer
Follow this Question
Related Questions
There is a generation lag 1 Answer
Why isn't my instance of an object moving? 1 Answer
Iterate through area descibed by objects 1 Answer
Instantiate object at position, moving in a direction? 1 Answer
Player invisible on respawn 2 Answers