- Home /
Make it Spawn 1 Cube instead of infenity
my scripting problem is on line 71/72 when it spawns the object it keeps spawning non stop i want to limit it to only 1
var drop : boolean = false;
var tunnelwidth = 500;
var tunnelheight = 200;
var tunnel1 = 100;
var tunnel2 = 30;
var law = 500;
var lah = 230;
var size = 100;
var size1 = 25;
var pickw = 500;
var pickh = 255;
var picksize = 100;
var picksize1 = 25;
var Cube : GameObject;
var clone : GameObject;
var clone1 : GameObject;
function OnGUI ()
{
if (details)
{
if (GUI.Button(Rect(tunnelwidth, tunnelheight, tunnel1, tunnel2), "Cube"))
{
detaillistActive = !detaillistActive;
}
if(detaillistActive)
{
GUI.Box(Rect(law, lah, size, size1), "Cube");
if (GUI.Button(Rect(pickw, pickh, picksize, picksize1), "Pick-up"))
{
pickup = !pickup;
}
}
}
if(pickup)
{
if (GUI.Button(Rect(100, 100, 75, 25),"Cube"))
{
dropdetails = !dropdetails;
}
if(dropdetails)
{
GUI.Box(Rect(law, lah, size, size1), "Cube");
if(GUI.Button(Rect(pickw, pickh, picksize, picksize1), "Drop"))
{
drop = true;
}
}
}
if (pickup)
{
details = false;
}
if (pickup)
{
Destroy (Cube);
}
if (drop)
{
pickup = false;
}
if (drop)
{
Instantiate (clone);
Instantiate (clone1);
}
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "Player")
{
details = true;
}
}
function OnTriggerExit(other: Collider)
{
if(other.tag == "Player")
{
details = false;
}
}
Answer by Yokimato · Oct 18, 2013 at 03:19 PM
The reason you're seeing this happen is that you're instantiating cube in the OnGUI
which is called once per frame. It's inside a if statement checking if drop
is true
, but drop is never turned to false, so each frame, after drop
is true, you create a new cube.
On a side note about code quality, you should probably brush up on programming skills outside the context of unity and then circle back once you have a solid foundation to build upon.
Example:
if(drop) {
//does A
}
if(drop) {
//does B
}
Can turn into:
if(drop) {
// does A
// does B
}
Since you already know drop is true.
it's easy to say to build up unity coding knowledge it's hard to point them where to learn, ...
I don't even know where would I point him to learn them.
but I would explain it to him here.
BTW to the questioneer:
OnGUI function is called every time something is changed so it could be called even 1000 times a frame depending on the code or just time 1 per few frames.
that's why puting random values inside OnGUI function makes it fuzzy
can you please just leave a answer that will help me and not how to organize my code it works perfectly fine other then it generating cube infinitely what do i need to add/change to make it only generate only 1
btw yokimato i did have it the 2nd way and the 1st way bc other wise script wldnt work correctly i had to make it 2 separate statements
No you didn't. That doesn't make any sense. If you read the first paragraph of the answer, you would have found the answer. I'm very much regretting helping you. As for sdgd, how about a general program$$anonymous$$g book on C#?? He clearly isn't a very strong programmer...
no I just started program$$anonymous$$g on start of 2013, ...
Your answer
![](https://koobas.hobune.stream/wayback/20220613120154im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Item Scripting Error 2 Answers
Setting Scroll View Width GUILayout 1 Answer
How do I correct the following error on the Lerpz Tutortial GUI 1 Answer
Tracking Down GUI Errors 0 Answers
"NullReferenceException" while trying to draw a texture (C#) 1 Answer