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 DarkoJocic · Sep 27, 2014 at 12:39 PM · variablebooleanaccesspublic

Script not responding to public variable change

So, I have a script which needs access to a variable from an another script. Accessing seems to be functional, however, Changing the variable state does not seem to affect the other script. Under this, is Script 1, initially containing the variable.

public class MMenuElements : MonoBehaviour {

 public bool ElementButton = false;


 void OnMouseDown()
 {
     ElementButton = true;
     Debug.Log("Button pressed");
 }

 void Update ()
 {
     if(ElementButton == true) 
     {
         Debug.Log("eButtton is true");
     }
 }

}

Quite simple. Next, there's the Script 2, which needs access to Script 1.

public class MMCam : MonoBehaviour {

private MMenuElements eButton;

 void Update () 
 {
     if(eButton.ElementButton == true)
     {
         gameObject.SetActive(false);
     }
 }

}

So, is there anyone who might have a clue at what's causing the problem?

Comment
Add comment · Show 5
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 767_2 · Sep 27, 2014 at 12:43 PM 0
Share

so what doesnt work

avatar image DarkoJocic · Sep 27, 2014 at 01:00 PM 0
Share

Right, right. Script 2 doesn't seem to be affected at all when I change the value of ElementButton within Script 1.

avatar image _dns_ · Sep 27, 2014 at 02:22 PM 0
Share

Hi, your code looks ok. $$anonymous$$aybe you could add a few Debug.Log to check if everything is called in the $$anonymous$$$$anonymous$$Cam code. I would also check that the eButton is assigned to the right element: maybe your are testing by clicking on another one than the one referenced in the $$anonymous$$$$anonymous$$Cam object ? To check this kind of thing I log the object's GetInstanceID().

avatar image DarkoJocic · Sep 28, 2014 at 08:22 PM 0
Share

Well, I seem to get an error: "Object reference not set to an instance of an object", originating from $$anonymous$$$$anonymous$$Cam script. I also do not seem to get any response from the if statement in the $$anonymous$$$$anonymous$$Cam script, however, both Debug.Log-s from the $$anonymous$$$$anonymous$$enuElements script seem to work normally. Any ideas? Edit: I also seem to have a warning, stating: "$$anonymous$$$$anonymous$$Cam.eButton is never assigned to, and will always have its default value null'"

avatar image DarkoJocic · Sep 28, 2014 at 09:05 PM 0
Share

It worked, thanks a lot :D

1 Reply

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

Answer by theredace · Sep 28, 2014 at 08:33 PM

So you have a private variable for script 1 in script 2, but I don't see it being assigned anywhere. You need to either make it public and assign it via the inspector, or assign it in your Start() method using something like GameObject.Find() or GameObject.FindGameObjectWithTag().

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

Getting a variable from another object 1 Answer

Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer

Accessing variable from a method in another script and gameObject 2 Answers

Access a variable from another script in update function 1 Answer

Help With C# Static Boolean! 2 Answers


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