gameobject doesn't spawn in the assigned random positions?
In my game I have a game object that is supposed of be transporting in random positions but it is instantiating in the empty game object that has the script this is my code ` using UnityEngine; using System.Collections;
public class thingSpawner : MonoBehaviour {
public GameObject thing;
public float MaxObjectCount = 0;
// Use this for initialization
void Start () {
StartCoroutine (TheSpawn ());
thing.SetActive (true);
}
// Update is called once per frame
void Update () {
}
IEnumerator TheSpawn()
{
while (true) {
yield return new WaitForSeconds(Random.Range(1,6));
thing.transform.position = new Vector3(Random.Range(-7f,5f),Random.Range(-4f,4f),Random.Range(0.7f,7f));
GameObject clone = (GameObject) Instantiate(thing, transform.position,Quaternion.identity);
Destroy(thing,2f);
}
}
}
Answer by Peaj · Sep 09, 2015 at 11:19 AM
It seems like you are moving your "thing" and then destroy it. But the new thing you instantiate gets the position of the object the script is attached to and not the position of your moved old thing. The position of "this" gameobject didn't change. So depending on your intention (if you want to move or delete/recreate the object) you could either just change the transform.position parameter of instantiate to thing.transform.position or just delete your Instantiate and Destroy lines.
but if I delete the instantiate line no game object will be created correct??? @Peaj
It depends on what you want. You could either just move the existing object or destroy it and instantiate it in a new position.
What you do right now is:
-$$anonymous$$ove the old object
-Instantiate a new one at the wrong position (not your random position)
-Delete the old one
If you just want to move the object around I would recommend to just move it and get rid of the cloning/deleting code.
If you want to stay with cloning the object (however it isn't necessary in most situations) just change the line "GameObject clone = (GameObject) Instantiate(thing, transform.position,Quaternion.identity);" to "GameObject clone = (GameObject) Instantiate(thing, thing.transform.position,Quaternion.identity);"
Answer by IHackedDeath · Sep 09, 2015 at 11:21 AM
Hi adhamyasser,
I have read through your question and I am not entirely sure on what you are asking?
I assume you want the object to continuously teleport to new random positions.
If so then I would recommend moving start coroutine (Thespawn()) to update instead of start as in the start function it is only called once.
Also I would recommend the same thing for where you change thing.setactive to move it into update.
If you would like to give a bit more information on what exactly you are after and maybe an example then I am happy to assist further.
I hope this helps.
Kind Regards,
IHackedDeath.
it was still instantiated in the same place and the instantiated object (thing) instantiated even faster any other ideas ??? @IHackedDeath
You should not start a coroutine in the Update method. StartCoroutine already makes sure that the function is called every frame. So Start is totally fine.