Instantiating GameObject loses some properties of the original GameObject
Hi! I am creating a simple ball game as my thesis and I encountered a strange problem. When the ball hits any hazard, it should explode and then create new ball to the respawn. It works fine the first time, but after losing a life, the ball doesn't disappear anymore and no explosion or respawn is instantiated and the ball just stays where it is. The HUD.Lives line works no matter how many balls get killed. Here is my code, been trying it bit differently many times, but no success:
void OnTriggerEnter(Collider other)
{
If (other.gameObject.tag == "Hazard") {
HUD.Lives = HUD.Lives - 1;
Instantiate(Explosion, GameObject.Find("AmigaBall").transform.position, Quaternion.identity);
GameObject AmigaBall = Instantiate(GameObject.Find("AmigaBall"),
GameObject.Find("Respawn").transform.position, Quaternion.identity) as GameObject;
Destroy(GameObject.Find("AmigaBall"));
GameObject.Find("AmigaBall(Clone)").name = "Amigaball";
AmigaBall.transform.localScale = new Vector3(2, 2, 2);
GetComponent<AudioSource>().Play();
}
}
Would appreciate any help I can get. I think it might be possible just to teleport the ball to the respawn instead of instantiating it and destroying the old one, but would like it to work somehow like this if possible.
or its actually just a Sphere, not an empty GameObject if it matters... Had the sphere under empty GameObject earlier, but there was bit confusion with that too so I changed it.
Answer by Warphead · Apr 20, 2017 at 05:51 AM
Now when I thought it a bit more, the topic should have been "Can't instantiate from instantiated GameObject". Well I think I am going to change the instantiation to just moving the GameObject when it loses life.
Your answer
Follow this Question
Related Questions
How to destroy on exiting PlayMode/EditMode? 1 Answer
I need help about Basic Basketball Game 0 Answers
What is Casting in Unity ? 1 Answer
RPG instantiate problem 3 Answers