Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
-1
Question by darkal · Oct 18, 2013 at 03:13 PM · guierrorpickup

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;
             }
         }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 9 · 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
avatar image sdgd · Oct 18, 2013 at 03:35 PM 0
Share

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

avatar image darkal · Oct 18, 2013 at 05:33 PM 0
Share

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

avatar image darkal · Oct 18, 2013 at 05:45 PM 0
Share

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

avatar image Yokimato · Oct 19, 2013 at 06:09 AM 0
Share

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...

avatar image sdgd · Oct 19, 2013 at 08:31 AM 0
Share

no I just started program$$anonymous$$g on start of 2013, ...

Show more comments

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.

Follow this Question

Answers Answers and Comments

17 People are following this question.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges