- Home /
Is a clone object still a (clone) if you change the name of it using script?
Is a clone object still a (clone) if you change the name of it using script? I want to create 5 enemies but they all have to have different names. I have one script that will act as the enemies AI. Will the objects/enemies that I create act as Individual objects/Enemies or act as One. I Really hope this makes since... lol Thanks guys!
Answer by clunk47 · Dec 07, 2013 at 01:41 AM
Each object has its own Instance ID and will not lose any kind of control or property by changing its name. For example if you want to Instantiate 5 prefabs or create 5 new GameObjects and name them according to their index in the array:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
GameObject[] cubes;
void Start()
{
cubes = new GameObject[5];
for(int i = 0; i < cubes.Length; i++)
{
cubes[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubes[i].name = "Cube" + i.ToString();
}
foreach (GameObject cube in cubes)
print (cube.name);
}
}
What I want to do is pool the enemies above the camera and then introduce the created enemies to the player at specific times during Gameplay by moving one specific enemy Jet by name.
I have the pooling done, but for some reason it is not finding the player in the enemy's AI script like it is suppose to do upon instantiating on start at the pooling site.
Also Should i force the created enemies in a new array and track them like that or should i just find each one by its name?
You can find them by name or index in the array.
By name:
foreach(GameObject cube in cubes)
{
if(cube.name == "whatever name")
{
//DoSomething
}
}
By index:
cubes[1]
$$anonymous$$eep in $$anonymous$$d indexes start with 0.
/////////////
As far as it not finding the player in the enemy ai script, i'd need to see the enemy ai script...
ToString() changes a value to a string. A GameObject's name property is a string, so you can't just name it "cube" + i, because i is an int. So if i is 1, i.ToString() would be "1" as a string. "Cube" + i.ToString() would be "Cube1".
Ok with a little tinkering I got the findtag && the LookAt working properly on Instantiate of all 5 enemies in the AI Script.
I think i am going to use the array but i need to force the object now into that array. I have another script that finds all objects in the scene with ObjectsOfTType and it searches through that. Is it possible to search by partial name? example; Jet_0, Search the entire scene for objects that name starts with Jet?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Example : $$anonymous$$onoBehaviour
{
List<GameObject> jets = new List<GameObject>();
void Start()
{
foreach (GameObject go in GameObject.FindObjectsOfType(typeof(GameObject)))
{
if(go.name.StartsWith("Jet"))
jets.Add(go);
}
foreach (GameObject jet in jets)
print (jet.name);
}
}
Look at String.StartsWith for more info.