• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by kryptonianwizard · May 05, 2016 at 06:51 AM · c#gameobjectinstantiaterandom.rangeclone

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");
     }
 }
 }





Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · May 05, 2016 at 08:19 AM 1
Share

Do you mean the scene changes too early? Can't see you calling Destroy() anywhere.

avatar image tuncOfGrayLake · May 05, 2016 at 08:58 AM 0
Share

As a side note, if you use lowerCamelCase to name your variables it should make things easier to read for everyone.

avatar image meat5000 ♦ · May 05, 2016 at 09:49 AM 1
Share

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.

avatar image RChrispy · May 05, 2016 at 09:59 AM 0
Share

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;
     }

avatar image kryptonianwizard · May 06, 2016 at 09:10 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need help about Basic Basketball Game 0 Answers

Attach a shield to player (C#) 2 Answers

Problems instatiating one gameobject but not others 0 Answers

local scale not working? 2 Answers

Instantiating random prefabs gives wrong position 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges