Object Instantiating to many objects

Hello, I have a gameobject that if a if statement applies to my script a object will Instantiate, but when i run the game it instantiates way to many and does not stop making the object what am i doing wrong. Here is my code. BTW i want to keep the way its setup but just only have it Instantiate the model one time.

public Transform Weapon1GO; public string Weapon1;

public GameObject Tac50;

public Transform WeaponSpawn;

void update(){

Weapon1 = "Tac50"; WeaponSpawn = transform.Find("Main Camera/WeaponWalkHolder/WeaponJumpHolder/WeaponSwayHolder/WeaponRecoilHolder"); if(Weapon1 == "Tac50"){ if(Weapon1GO == null){ GameObject weapon1I = Instantiate(Tac50, transform.position,transform.rotation) as GameObject; weapon1I.transform.parent = WeaponSpawn;

} }

Update I know why this is happening but i dont know how to stop the instantiating, ive tried bools. Please some one tell me how to fix this.

more ▼

asked Mar 25, 2013 at 03:11 AM

avatar image

559 46 1858 186

Please format your code. If you do not know how, watch the tutorial video and look at the FAQ: http://video.unity3d.com/video/7720450/tutorials-using-unity-answers http://answers.unity3d.com/page/faq.html

Mar 25, 2013 at 03:21 AM Benproductions1
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

All right, if I'm reading your code correctly then you have two if statements. The first if statement is to check and see if Weapon1 == "Tac50". But that if statement is kind of a moot point since you're defining Weapons1 = "Tac50" at the beginning of every update. And then the next if statement is Weapon1GO is null, and since I don't see it being set to anything at all at any point, then both these if statements are going to remain true at all times.

So a new object will be instantiated at every frame. You need to make one of those if statements false when you get into them. You've basically made an infinite loop with if statements. I can't really tell you how to fix the problem since I'm not sure what this is supposed to do or how it all works together with the rest of your project. But that's where I see your problem residing.

more ▼

answered Mar 25, 2013 at 03:29 AM

avatar image

175 10 11 16

Yes I know that its being instantiated every frame but how can i have it not i tried i bool, tried making the second if statement false, but nothing will work and i cant change the first if statement does any one know how to fix this

Mar 25, 2013 at 03:59 AM HuskyPanda213

Oh and Btw Thanks for the answer

Mar 25, 2013 at 03:59 AM HuskyPanda213

If you want a quick and dirty fix. Make a new boolean variable. And make the second if check:

 if(Weapon1GO == null && !newBool)
      newBool = false;
      //Do all other things here

That will stop it from running again after the first time.

Mar 25, 2013 at 04:42 AM Nerull22
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 25, 2013 at 03:11 AM

Seen: 487 times

Last Updated: Mar 25, 2013 at 04:42 AM