Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 brunomont · Nov 28, 2018 at 09:43 AM · button trigger eventsreferencingprivateassign-variable

Access private variable from UI Event

Hey guys, I am trying to use a private variable inside a public function that is used as the Button Click event, but it is not working.

Here is what I have it currently setup:

 private GameObject myGameObj;

 void Awake(){
     myGameObj = GameObject.Find("game_object_name");
 }

 // This function works fine, myGameObject becomes inactive
 void Start(){
     myGameObj.SetActive(false)
 }
 
 public void myFunction(){
     myGameObj.SetActive(true);
 }

And in my UI Button, in the click event I am calling myFunction. I know the reference for myGameObj is correct because during the Start event, I am setting it to be inactive and that works just fine. But when I try to make it active again (using a button event) it does not work!

My first though was some issue related to the GameObject be inactive, but I removed the SetActive code and tried to only print the reference using Debug.Log.

Printing it during the Start event shows the right reference. Printing it during the myFunction event shows a null reference.

Any ideas? Thank you!

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 Hellium · Nov 28, 2018 at 10:00 AM

  1. Are you sure the object is not destroyed between Start and myFunction?

  2. Are you sure the function called is the "correct one" (meaning you don't have attached the script twice and used the object not calling the Start method)

  3. Are you sure myFunction is not called before Awake (it can happen if the gameObject is not active when the scene starts)

Use Debug.Log to help you find the issue

  private GameObject myGameObj;
  void Awake(){
      myGameObj = GameObject.Find("game_object_name");
      Debug.Log( "myGameObj retrieved : " + myGameObj, myGameObj ) ;
  }
  // This function works fine, myGameObject becomes inactive
  void Start(){
      myGameObj.SetActive(false);
      Debug.Log( name + " is disabling myGameObj", myGameObj ) ;
  }
  
  public void myFunction(){
      myGameObj.SetActive(true);
      Debug.Log( name + " is enabling myGameObj", myGameObj ) ;
  }
Comment
Add comment · Show 1 · 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 brunomont · Nov 28, 2018 at 11:29 AM 0
Share

Thank you, that did the job! For some reason the function reference got swapped. It was pointing to the prefab ins$$anonymous$$d of the GameObject in scene.

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

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

public vs private array in classes 0 Answers

Building Scripts on each other 1 Answer

I have 12 objects using the same script below, I need them to each activate individually of each other. 0 Answers

Custom geometry image hover 0 Answers

Triggering UI button onmousedown, without waiting for mouseup event? 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