Search Unity

Unity says Variable is Unassigned when I assigned it

Discussion in 'Editor & General Support' started by cloa513, Jan 14, 2022.

  1. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79

    upload_2022-1-14_21-14-32.png


    As you can see I assigned Canvas1 with canvas but Unity says it is unassigned. Please explain.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Nobody here can explain. We can suggest things for you to look at.

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    are you trying to access the canvas component or the canvas game object?
     
    JeffDUnity3D likes this.
  4. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79
    Thank you for your reply do you mean a canvas module. No I not trying to access a canvas module. It is the canvas game object (the one you might call the main canvas) - the one often listed on the side near the Main Camera and I am trying to instantiate onto it.
    Not null because it is listed in the Inspector as you can see.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    Unity doesn't lie about null reference exceptions. If it's saying something is null, it's null. You haven't shown the code in question, so you're going to need to post it if we're going to be able to help you.
     
    Kurt-Dekker likes this.
  6. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79
    Code (CSharp):
    1. sing System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CreateButtons : MonoBehaviour
    7. {  [SerializeField] private Button1 ts;
    8.     private Button1 a;
    9.     public Canvas canvas1;
    10.     public Values value1;
    11.     private float _px;
    12.    
    13.     private float _py;
    14.  
    15.     private string _rowcol;
    16. //    private int _tempnum;
    17.     private int _num;
    18.  
    19.     private int _row;
    20.  
    21.     private int _col;
    22.  
    23.     private int _squ;
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.         for (var i = 0; i < value1.col*3; i++) //value1.row is number of rows starting value 9
    28.         {
    29.            
    30.             for (var j = 0; j < value1.row*3; j++) //value1.row is number of columns starting value 9
    31.             {_py = ((value1.yst + value1.ys * (i)));
    32.                 _px = (value1.xst + value1.xs * j);
    33.              
    34.                
    35.                
    36.                             a=Instantiate(ts,new Vector3(_px,_py),Quaternion.identity);
    37.                             a.transform.SetParent (canvas1.transform,false);
    38.                             ;
    39.             }
    40.                            
    41.  
    42.         }
    43.     }
    Can't Unity Inspector permit something that is not allowed by the Unity Game Engine?
     
  7. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    is easy to miss if you are not familiar with the editor interface

    in your first screenshot the object assigned in the Canvas1 slot is of type Canvas not GameObject. It doesn't have the gameobject icon but the canvas icon.

    the code you posted confirms this as your variable is of type canvas not gameobject

    The reason you didn't got any errors when you have dragged the game object from scene to the variable slot is that if you create a variable that is of type of a game object component (transform, collider, mesh all of these types) and you drag a game object to the variable slot, unity will assign to the slot the component of that type that is present on the game object.

    You can drag the component directly to the slot, in your case the canvas component, will work the same. for convenience unity will let you drag the entire object also. Is a shortcut.

    in your code change the Canvas1 to be of type GameObject then drag the Canvas1 game object to the slot again.

    Pay attention to the icons.

    You can learn more about various types of components and objects in unity in the script reference

    https://docs.unity3d.com/ScriptReference/Canvas.html
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    Uh, it really doesn't make a difference if you assign a reference to a game object or a component, as components all have a .gameObject and .transform property.

    I don't think there's anything wrong with the code and how the fields are reference. I tested some similar code below which all worked as expected:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CreateButtons : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private Button buttonPrefab;
    10.  
    11.     [SerializeField]
    12.     private Canvas canvasParent;
    13.  
    14.     [SerializeField]
    15.     private int buttonSpawnAmount = 10;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         for (int i = 0; i < buttonSpawnAmount; i++)
    21.         {
    22.             Button spawnButton = Instantiate(buttonPrefab, Vector3.zero, Quaternion.identity);
    23.             spawnButton.transform.SetParent(canvasParent.transform, false);
    24.         }
    25.     }
    26. }
    27.  
    I'm starting to suspect OP has another instance of the component with unassigned references somewhere in the project.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    The one you're looking at... WHEN you're looking at it.

    How about other ones (do you have others that you accidentally dragged on?) and what about WHEN... not everything happens in the order you think it does. See my post on using Debug.Log() to learn more.

    EXACTLY what Spiney says. Something is null. Might not be the thing you are insisting ISN'T null, but that's up to you to determine using the steps listed above.
     
  10. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79
    It is not a Null object- it is an unassigned statement- the latter is an Unity statement not runtime statement.
     
  11. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79
    By the way, my inspector won't allow by the drop down menu to add Game Objects- only assets- I assigned the canvas to canvas1 by drag and drop . Is that a bug?
     
  12. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    No there should be two tabs up the top for an asset or scene object:
    upload_2022-1-16_13-33-1.png
     
    cloa513 likes this.
  13. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79
    Thank you. I am not completely sure if it was there before but it is definitely is there now and now that variable doesn't bug.
     
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    So you were adding an asset level reference before? That would definitely cause an error I would think.
     
  15. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79
    No I dragged the icon from the Game Object area. As you can see Inspector accepts it but doesn't recognize it.
     
  16. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    I has a similar error right now and the reason was that I had my script on another gameobject that I created by copy/paste the original one. So can you make sure, that there are no other script of the type CreateButtons on any other gameobject? Maybe on the canvas object for example.