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 cidmodder · Jun 13, 2012 at 03:04 PM · c#oncollisionstaygetmousebutton

Instantiate only One Object

I am trying to be able to place boards down when they collide with other objects and you click. However if it collides with more than one object it places more than one board in the same spot. How can I change it so that it only places one board? This is the script on the plank that you use to decide where to place it.

 void OnCollisionStay(Collision collisionInfo){
     
         if (Input.GetMouseButtonDown(0) && placeone == false){
         
             Instantiate(woodPlank, transform.position,transform.rotation);
             placeone = true;
         }
         
         if (Input.GetMouseButtonUp(0)){
             
         placeone = false;    
         }
     
     }

shouldn't that do it? Thanks for any help!

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 Wolfram · Jun 13, 2012 at 04:11 PM

Your problem is that you have multiple scripts, all reacting to the same Input.GetMouseButtonDown(0), and each spawning an object.

The easiest solution is to make the bool "placeone" static:

 static bool placeone;
 // your code

This way, there is only one instance of this bool, which is known (and identical) to all script instances of this class.

Comment
Add comment · Show 6 · 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 cidmodder · Jun 13, 2012 at 04:25 PM 0
Share

there is only one object that actually has the script to instantiate the planks. the others have no script right now to avoid that kind of error. that one object though is spawning more than one board if it collides with more than one object.

avatar image Wolfram · Jun 13, 2012 at 04:48 PM 0
Share

O$$anonymous$$, I misinterpreted the problem, but the solution is still valid. From the docs: "OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.".

avatar image cidmodder · Jun 13, 2012 at 05:05 PM 0
Share

that worked but i still dont understand why making it static fixes it if the only script that has the variable is the object that places the plank.

avatar image Wolfram · Jun 13, 2012 at 05:13 PM 0
Share

Hm, now that you mention it, you are right, it shouldn't make a difference - unless there is more than one instance of this script (or an object with this script attached) in your scene at runtime. Are you absolutely certain that is not the case?

avatar image Wolfram · Jun 13, 2012 at 05:22 PM 0
Share

You can verify that by putting your game in Play mode (in case you are instantiating other stuff on-the-fly), then right-click on the script in the Project tab and select "Find References in Scene". Now there should only be one object listed in the Hierarchy tab.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I use OnCollisionStay? 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 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