Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Mystique5 · Jan 28, 2017 at 02:34 AM · unity 5gameobjectsenable and disable script

How to enable only one script (shared by multiple gameobjects) at a time/click ?

So I have two scripts each attached to a few blocks (gameObjects). Scripts are: DragScript and CheckWin

DragScript actually enables the CheckWin script when the gameobject its attached to is clicked.

 public class DragScript : MonoBehaviour {
     public CheckWin scriptCheckWin;
     public static bool mySwitch = true;
 
         void OnMouseDown()
         {
     
             if (Physics.Raycast(ray, out rayHit))
             {
                 if (rayHit.collider.tag == "Puzzle")
                 {
                     scriptCheckWin = gameObject.GetComponent<CheckWin>();
                     if (mySwitch == true)
                     {
                         scriptCheckWin.enabled = true;
                         mySwitch = false;
                     }
                     else {
                         scriptCheckWin.enabled = false;
                         mySwitch = true;
                     }
     
                 }
                 
             }
         }

This lets me turn enable and disable the scriptCheckWin on the gameObject -by clicking it- the dragscript is attached to (like it should)

The problem is if I click another gameobject its scriptCheckWin is enabled and the previous one I just clicked is also enabled. I only want one scriptCheckWin to be enabled when clicked to be enabled for efficiency. Just to let you know there will be more than 2 gameobjects.

Thanks in advance.

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
1
Best Answer

Answer by AR_Rizvi · Jan 28, 2017 at 05:46 AM

Hope this Will help Edited your Code

 public class DragScript : MonoBehaviour {
      public CheckWin scriptCheckWin;
      public static bool mySwitch = true;
      public static Gameobject lastCheckWin;
 
          void OnMouseDown()
          {
      
              if (Physics.Raycast(ray, out rayHit))
              {
                  if (rayHit.collider.tag == "Puzzle")
                  {
                   if(lastCheckWin ==null)
                      {
                      scriptCheckWin = gameObject.GetComponent<CheckWin>();
                      lastCheckWin = gameObject.GetComponent<CheckWin>();
                       }
                   else
                   {
                   lastCheckWin.GetComponent<CheckWin>().enabled  = false;
                   lastCheckWin = gameObject.GetComponent<CheckWin>();
                    }
                      if (mySwitch == true)
                      {
                          scriptCheckWin.enabled = true;
                          mySwitch = false;
                      }
                      else {
                          scriptCheckWin.enabled = false;
                          mySwitch = true;
                      }
      
                  }
                  
              }
          }
 
 
Comment
Add comment · Show 4 · 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 Mystique5 · Jan 28, 2017 at 01:19 PM 0
Share

While I do think you are possibly on the right track (in terms of logic) it does not seem to work for me as the gameobject cannot be null. I changed it to be a CheckWin (script) variable but even then it does not accept null.

avatar image Bunny83 Mystique5 · Jan 28, 2017 at 01:47 PM 2
Share

Null should be null

avatar image Mystique5 Bunny83 · Jan 30, 2017 at 03:04 AM 0
Share

$$anonymous$$y sincere thanks to the both of you guys. You saved me a lot of time and head scratching. I can not thank you enough.

avatar image AR_Rizvi · Jan 31, 2017 at 10:40 AM 0
Share

i Edit it and sorry for not testing the code Cheers

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

104 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 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 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 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 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 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 avatar image avatar image

Related Questions

Best practices to select a group of gameObjects 1 Answer

Unity5/C# beginner here! Need some scripting help :) <3 1 Answer

How to assign a gameobject/transform to a prefab 0 Answers

How to toggle elements within a prefab in Unity 0 Answers

How do I enable/disable components in a prefab from another script? 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