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
0
Question by qhenckel · Apr 19, 2015 at 11:35 PM · gameobjectinstantiateclonepublicshared

Variable being shared between clones

 var selected : boolean = false;
 ..
 function Update () {
     ..
     if(Input.GetMouseButtonDown(0)){
         if(Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition))) {
             selected = !selected;
         }
     }
     ..
     if(Input.GetKeyDown("g")) {
         Instantiate(GetComponent("Transform"));
     }
 }

All clones of the GameObject toggle their selected state when anyone is clicked. Thanks in advance.

Comment
Add comment · Show 3
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 qhenckel · Apr 20, 2015 at 01:29 PM 0
Share

Thanks for the answers!
@DoTA_$$anonymous$$A$$anonymous$$I$$anonymous$$ADzE
I want the opposite of what you said.
I want to have each clone select and deselect independently.
I asked the question because each clone should be doing this already, but they aren't.

@Bored$$anonymous$$ormon
If line 6 works like I think it does:

 if(Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition))) {

That 'if' should only be true when the clone has the mouse on it.
So only that clone should toggle its selected state.
However the problem I am having is that ALL clones toggle their selected state when I click on one.

Thanks Again!

avatar image DoTA_KAMIKADzE · Apr 20, 2015 at 02:21 PM 0
Share

Check out my updated answer.

avatar image Bonfire-Boy · Apr 20, 2015 at 02:34 PM 1
Share

Check the documentation for Physics2D.OverlapPoint and notice that there's nothing in your if statement that relates specifically to the current object.

Physics2D.OverlapPoint returns any collider it finds that overlaps the point. In your context, an overload causes that collider to be cast to a bool, so that null (no collider found) is interpreted as false and anything else as true. So your if statement is true regardless of which collider it finds.

So, using your method of having this in the update functions of the individual objects, you'd need to look at that collider and check if its the one on the current object.

Or, better, put this update in some other (singular) object and have that call a method on the (gameobject associated with) the collider it finds, that way you'd only be calling the relatively-expensive OverlapPoint function once per frame.

2 Replies

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

Answer by DoTA_KAMIKADzE · Apr 19, 2015 at 11:36 PM

Ah well then I got you wrong initially. I think I got you right now ;)

Do this:

     Collider2D coll;
     private void Awake()
     {
         coll = GetComponent<Collider2D>();
     }
     void Update () 
     {
         if (Input.GetMouseButtonDown(0))
         {
             if (coll.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition)))
             {
                 selected = !selected;
             }
         }
     }
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
avatar image
0

Answer by Kiwasi · Apr 19, 2015 at 11:43 PM

That's because you are doing this in Update. Update runs on every GameObject every frame. You are checking for a mouse down event, that happens any time the user clicks anywhere.

Fix this by using OnMouseDown instead of Update.

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.

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

Assign an instantiated GameObject? 1 Answer

Reference an Instance of an Object?C# 1 Answer

Rotate a cloned object with key press 1 Answer

instantiated gameobjects(clone) don't have components of its original gameobject. 1 Answer

How to create a clone of an object with a different name 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