Random.range game object destroying itself before reaching destroy position.Please help.
I have started cloning objects from a random range on my y axis.but they are getting destroyed before reaching the destroy position.Need help,really fast.
here is the generator script;
using UnityEngine;
using System.Collections;
public class generate : MonoBehaviour {
public GameObject clone,asteroid1;
public GameObject[] asc= new GameObject[3];
public float createrate = 0.5f;
private float nextcreate = 0.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (Time.time > nextcreate)
{
nextcreate = Time.time + 1.2f;
clone = Instantiate (asc[Random.Range(0,3)] , transform.position, transform.rotation) as GameObject;// create a duplicste of an object in the position of the "clone".
//Vector3 position = new Vector3(Random.Range(-10.0F, 10.0F), 0, Random.Range(-10.0F, 10.0F));
clone.transform.position = new Vector3 (10.875F, Random.Range (6.775F, -7.15F), 0);
}
}
}
Destroy Script;
using UnityEngine;
using System.Collections;
public class gameover : MonoBehaviour
{
public GameObject colt2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D colt2)
{
if (colt2.gameObject.tag == "border")
{
Application.LoadLevel("game_over");
}
if (colt2.gameObject.tag == "ast")
{
Application.LoadLevel("game_over");
}
}
}
Do you mean the scene changes too early? Can't see you calling Destroy() anywhere.
As a side note, if you use lowerCamelCase to name your variables it should make things easier to read for everyone.
So I guess you have multiple objects and you cant work out why they are all being 'destroyed' at the same time as the one that actually gets the target? Or something to that effect? Reloading a scene will Destroy ALL objects and recreate them.
yeah like the others said i guess the scene load is the problem here?
Also they dont move do they? you spawn them exactly at the random range position and thats it.
Another question here from me: isn't such a timer bad because of not infinit float size?
if (Time.time > nextcreate) {
nextcreate = Time.time + 1.2f;
}
This one should be better or am I wrong?
nextcreate += Time.deltatime;
if (nextcrate > 1.2f) {
nextcrate = 0f;
}
Actually,I realized while testing the game that the object is not destroying but its going invisible.
There is also a moveleft script attached to each prefab in that generator array,so that the move left. There is also a destroy script attached to those each prefab where they get destroyed after reaching a position on y axis. I have actually forgot to mention that I have built the whole game on Unity 4.5 and then I used Unity 4.6 to create GUI Text for displaying score and other collectibles. I feel that this has happened because of change in the Unity Version. Still Confused.Need Help. Thank you in Advance.
Answer by RChrispy · May 06, 2016 at 11:43 AM
I dont see any way this will work:
Application.LoadLevel("game_over");
You should call:
Destroy(this.gameobject);
The reason i suggest this is because it is a "destroy" script and not a "change scene" script! If you want to change the scene you should not use this frequently! Application.LoadLevel is not designed and not intended to be used that way. It is just to frequent.
Your answer
Follow this Question
Related Questions
I need help about Basic Basketball Game 0 Answers
Instantiating GameObject loses some properties of the original GameObject 1 Answer
What range of values need to be inserted if we need to pull a random item from the list? 0 Answers
How to save and load all gamebjects which are cloned at runtime???? 0 Answers