• 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 theTalorian · Aug 16, 2018 at 12:23 AM · instancevarible

Variable not actually assigned?

I'm sorry, I know something like this has been asked about a billion times, but I haven't managed to find a version of the question that seems to match my circumstances, so here's to a billion and one.

Short version: I'm creating instances of a prefab with a custom "UIButton" class assigned to it. I'm declaring variables for the class at the top as normal, and assigning them in Start(). This works, and I can use those variables in the Start method, but when I call another method for that instance I get the "variable has not been assigned" error.

Long version: I have a simple class that gets assigned to a button prefab:

 public class UIButton : UIElement {
     // Our pieces
     [SerializeField]
     GameObject upIndicator;
 
     // Use this for initialization
     void Start () {
       upIndicator = transform.Find("Up").gameObject;
       upIndicator.SetActive(false);
     }
 
     public void ActivateUp() {
         upIndicator.SetActive(true);
     }
 
 }

(I've simplified it a bit for the question, but that's the gist)

The prefab consists of a button with an extra image object added to it, named "Up". The plan was to have "up" available by default from the prefab, but automatically turned off on initialization, ready to be reactivated on instances that need it.

So I create several instances of my button, and for each one of them the Start() function properly finds the child "up" image, assigns that GameObject reference to the "upIndicator" variable, and then sets its active to false. This works great.

But when I get to the instance that actually wants to use the up indicator I call the ActivateUp() function and the script errors out claiming that the variable upIndicator has not been assigned. It was assigned, it worked long enough to deactivate it, and it's showing up properly in the inspector as being assigned to the "Up" image object child of that instance, but as far as the function is concerned it is null.

The issue exists even if I set that variable to public. I tried changing the code to use this.upIndicator, even though it shouldn't need it, but that didn't work. I've even tried reversing the logic of the thing completely, on the off chance that it was losing the variable assignments after setting the gameObject to inactive.

I'm wondering if it is somehow losing the reference to the correct UIButton instance, but it doesn't seem like it would be. I even tried it on a direct call:

         UIButton newButton = Instantiate(buttonPrefab).GetComponent<UIButton>();
         newButton.ActivateUp();

Even with that my up indicator is being turned off on instantiation, never to be seen again.

I have been racking my brain trying to sort this out, so any help would be greatly appreciated.

Comment
Add comment · Show 1
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 oStaiko · Aug 16, 2018 at 02:16 AM 0
Share

Here, I think I might know the problem, but add this to your script so we can find out!

 int count = 0;
 
 Update ()
 {
     if (upIndicator)
         Debug.Log(count++ + ":\nPASS");
     else
         Debug.Log(count++ + ":\nFAIL");
 }

This will tell you exactly which frame the reference is lost, and will point you as to where its being removed. At start, you know the reference is there. So if the first entry is FAIL, then its being loss somewhere during another scripts Start function, or possibly and early Update. If its the second time, then we at least know its not during Start, but an Update.

Also, serializing forces a variable to be public in some ways. make it private and remove the tag, repeat the test and lets see the results. If its still being removed, its because another script is calling your start function, you are deleting the reference in the script, or somehow the up object is being destroyed and possibly replaced without the reference being set again. If it's one of these, post your whole script and Ill verify it isn't that option.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gvergidis · Aug 21, 2018 at 12:12 PM

Why you use the find function? I mean, the UP object is a child of the instantiated object, correct? The why do not you reference to that object from the inspector? Prefabs that refer to objects in the prefab, do not lose that reference and will be instantiated too. So... drag the prefab in the scene, reference the UP object at the inspector and you click Apply to save the prefab. You will be fine! Make you life easier :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

90 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

Related Questions

How can I modify a variable on an instance of a prefab after I created it? 2 Answers

Create an Instance? 2 Answers

Unity importing Instances wrong via FBX. 4 Answers

Calling a function across all instances of an object 2 Answers

Is every Script I drag on a Gameobject an instance? 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