Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

HideInInspector, UnassignedReferenceException, with parent child classes

Discussion in 'Scripting' started by theDawckta, Nov 12, 2016.

  1. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    49
    Hello Ya'll,

    Wondering if I maybe stumbled upon a bug or hopefully someone can tell me what I am doing wrong.

    I have a public variable I am trying to hide in the inspector in a Parent class. I set this variable in the Child classes but I get the UnassignedReferenceException at run time which breaks my project. I thought HideInInspector prevented this exception.

    I am creating a new GameObject in the inspector and placing the Child script on it. How can I make this work?

    Code (CSharp):
    1.  
    2. public class Parent : MonoBehaviour
    3. {
    4.     [HideInInspector]
    5.     [NonSerialized]
    6.     public Transform myTransform;
    7.  
    8.     protected virtual void Awake()
    9.     {
    10.         if (myTransform != null)
    11.             myTransform.position = Vector3.zero;
    12.     }
    13.  
    14.     protected virtual void FixedUpdate()
    15.     {
    16.         AddRotation();
    17.     }
    18.  
    19.     void AddRotation()
    20.     {
    21.         Quaternion rotate;
    22.  
    23.         rotate = Quaternion.FromToRotation(Vector3.up, myTransform.transform.position - transform.position);
    24.         transform.localRotation = Quaternion.RotateTowards(transform.localRotation, rotate, 10.0f);
    25.         transform.localEulerAngles = new Vector3(0.0f, 0.0f, transform.localEulerAngles.z);
    26.     }
    27. }
    28.  
    Code (CSharp):
    1.  
    2. public class Child : Parent
    3. {
    4.     protected override void Awake()
    5.     {
    6.         base.Awake();
    7.     }
    8.  
    9.     protected override void FixedUpdate()
    10.     {
    11.         myTransform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
    12.         base.FixedUpdate();
    13.     }
    14. }
    15.  
     
    Last edited: Nov 14, 2016
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    First off the code you have in the Child class shouldn even work because you did not Override the Parent class's Awake, plus their access level (public vs private) don't match

    secondly HideInInspector only does what it says, hides a specific variable from being normally shown in the inspector. unity will still serialize it if its public or flagged with the SerializeField attribute. its pretty much just a UI thing, it doesn't protect you from programming errors.

    finally, the issue that you are getting here is thatyou've declared myTransform, but you haven't defined it. is it null? does it refer to its own transform, does it refer to some other transform. the complier doesn't know. you need to explictly set myTransform to a value before you can set its properties like position. like

    Code (CSharp):
    1.  
    2. public class Parent : MonoBehaviour
    3. {
    4.    [HideInInspector][NonSerialized]
    5.    public Transform myTransform;
    6.  
    7.    protected virtual void Awake()
    8.    {
    9.         if(myTransform != null)
    10.              myTransform.position = Vector3.zero;
    11.    }
    12. }
    13.  
    14. public class Child : Parent
    15. {
    16.     protected override void Awake()
    17.     {
    18.         myTransform = transform;
    19.         base.Awake();
    20.     }
    21. }
     
  3. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    49
    Thank you for the reply

    I applied what you have showed and it is working but my error is actually further down when I start trying to use myTransform. I have added the revised code to show a little better what I am trying to do. I am thinking I need myTransform to be a Vector3.

    Does using a transform create another game object? If so I think I may just have to use a Vector3 instead of a transform.
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    Yes, a transform is a component on a gameobject, not just a value by itself. You can reference a transform on a gameobject, but you can't create a new one. (without the a gameobject).
     
  5. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    49
    Thanks that explains it, I have changed what I have above to a Vector3 instead of a Transform and with additions elsewhere in my code I have this working. Thanks for the help guys.