- Home /
c# - Rename Instatiated GameObject - Solved
I am trying to make a tower defense game. At the moment i am trying to get the enemies to spawn. I can get them to spawn just find but the problem is that i cannot seem to change their name. This is my code:
private int spawnTime = 5;//seconds between enemies private float timer = 0;//countdown public Transform enemy;
void Start(){
timer = spawnTime;
}
void Update(){
if(timer>0){
timer -= Time.deltaTime * 2;
}else if(timer==0 || timer<0){
timer = spawnTime;
Spawn();
}
}
void Spawn(){
GameObject newEnemy = (GameObject)Instantiate(enemy, transform.position, Quaternion.identity);
newEnemy.name = "test";
//newEnemy.transform.position = new Vector3(0f, 0.5f, 0f);
}
Can anyone tell me how i can rename the instantiated GameObject?
EDIT: After editing my code i found that nothing after,
GameObject newEnemy = (GameObject)Instantiate(enemy, transform.position, Quaternion.identity);
is "executing".
is this the exact code?
in this line:
GameObjectnewEnemy = (GameObject)Instantiate(enemy, transform.position, Quaternion.identity);
you need a space between GameObject and newEnemy.
GameObject newEnemy = (GameObject)Instantiate(enemy, transform.position, Quaternion.identity);
You have no compiler errors?
I know there needs to be a space there(That's a typo...). (Still doesn't fix my problem.)
I have tried both of those ideas and they still don't seem to work.
Answer by 73cn0109y · Jul 14, 2013 at 02:39 PM
Sorry i fixed the problem. I had a "type mis-smatch" on my script. Changing the newEnemy type from transform to GameObject worked. Thankyou.
I have the same problem but even when I create newEnemy with type GameObject ins$$anonymous$$d of transform it gives the same error. Could you provide example code how it works for you?
Answer by Bunnybomb7670 · Jul 14, 2013 at 01:27 PM
Possibly because your instantiating a transform as a gameobject? make the enemy a GameObject instead of a transform.
Your answer
Follow this Question
Related Questions
Spawn Script 1 Answer
Distribute terrain in zones 3 Answers
Separate GameObject name? 2 Answers
How to SetTrigger all colliders with a specific name 1 Answer
(C#)Place GameObject from Mouse 0 Answers