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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Only primitive can be set in script

Discussion in 'Scripting' started by goddatr, Mar 26, 2018.

  1. goddatr

    goddatr

    Joined:
    Mar 16, 2018
    Posts:
    26
    What I'm trying to do is to link an Object (datas) to a GameObject
    I have an object, let's call it AnObject.
    This object has a GameObject as property as long as other properties.

    In the scene I want to access some properties of the AnObject when the corresponding GameObject is clicked.

    To do so I tried to add to the GameObject a MonoBehaviour script, called AnObjectData that only contains the corresponding instance of AnObject. This script is set as a Component and is initialized in AnObject's constructor.

    Here's the structure of my objects

    AnObject constructor :
    Code (CSharp):
    1. public AnObject(string name, string id, Vector3 position, Quaternion rotation, GameObject model)
    2.         {
    3.             //Construct object
    4.             // [...]
    5.             //Initialize the Monobehaviour with newly created AnObject
    6.             this.Model.GetComponent<AnObjectData>().setAnObject(this);
    7.             this.Model.GetComponent<AnObjectData>().setName();
    8.         }
    AnObjectData :
    Code (CSharp):
    1. public class AnObjectData : MonoBehaviour {
    2.  
    3.     public AnObject anObject;
    4.     public string Name;
    5.  
    6.     public void setName()
    7.     {
    8.         //Uses the AnObject's Name property
    9.         this.Name = this.anObject.Name;
    10.     }
    11.  
    12.     public AnObject getAnObject()
    13.     {
    14.         //Print null
    15.         Debug.Log(this.anObject);
    16.         return this.anObject;
    17.     }
    18.  
    19.     public void setAnObject(AnObject anObject)
    20.     {
    21.         this.anObject = anObject;
    22.         //Display the anObject's ToString properly (not null at this time)
    23.         Debug.Log(this.anObject);
    24.     }
    25. }
    The problem is when I try to access the AnObject with getAnObject() in AnObjectData or with the property it always returns null. But when I access the Name property (a string primitive) I got the actual value.

    I added some Debug.Log to see if the properties are initialized and it appears they are. But when I try to access them from outside they diseapear (see comments)

    How can I retrieve my properties ?


    EDIT : Forget to mention that I try to recover the AnObject instance like this :
    Code (CSharp):
    1.  
    2. activeAnObject.GetComponent<AnObjectData>().getAnObject();
    3.  
     
    Last edited: Mar 26, 2018
  2. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    It looks like you made the constructor, but have never actually called it. If you want AnObject inside AnObjectData to exist automatically, you need to give the field a default value. So:

    Code (CSharp):
    1. public class AnObjectData : MonoBehaviour {
    2.     public AnObject anObject;
    3. ...
    Becomes:

    Code (CSharp):
    1. public class AnObjectData : MonoBehaviour {
    2.     public AnObject anObject = new AnObject(...);
    3. ...
    If you're not planning on having all values populated at construction, I would highly suggest making a default constructor for AnObject.
     
  3. goddatr

    goddatr

    Joined:
    Mar 16, 2018
    Posts:
    26
    I've added a default constructor
    Code (CSharp):
    1. public AnObject(){}
    But now when I print the value of anObject by AnObjectData it only prints an empty object.
    Even if I set it before with setAnObject(anObject).
    The part strange part is that in
    Code (CSharp):
    1. public void setAnObject(AnObject anObject){
    2.         this.anObject = anObject;
    3.         Debug.Log(this.anObject);
    4. }
    The Debug.Log line shows the right value but when I call
    Code (CSharp):
    1. public AnObject getAnObject()
    2.     {
    3.         Debug.Log(this.anObject);
    4.         return this.anObject;
    5.     }
    It returns and print an empty object.

    the property seems to be erased before I call it.
     
  4. goddatr

    goddatr

    Joined:
    Mar 16, 2018
    Posts:
    26
    I've tried with different objects, it works (value is correctly set) for some but not for the other :
    • Works : int, string, int[], float, GameObject, Vector3
    • Does not work: ArrayList, AnObject
     
  5. goddatr

    goddatr

    Joined:
    Mar 16, 2018
    Posts:
    26
    Problem solved.

    AnObject should extends ScriptableObject