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

[SOLVED] Passing values from parent script (non-static) to other scripts

Discussion in 'Scripting' started by nasos_333, Oct 5, 2014.

  1. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    Hi,

    I want to pass a non static variable from one script that extends monobahavior to a class script that does not. Specifically i want to pass a gameobject prefab to the new script, which is a variable in the parent script.

    I create the instance of the other class with "new" and have declared a variable with the script type in the other script.

    This is the class that is going to the root object as component on the scene.

    Here is the exact code

    The controller
    Code (csharp):
    1.  
    2.  
    3. public class ControllerA : MonoBehaviour {
    4.  
    5. private Child_to_control New_child;
    6.  
    7. void Start () {
    8.  
    9.      New_child = new Child_to_control();
    10.      New_child.ThisController = this;
    11.  
    12.     }
    13. }
    14.  
    The Child_to_control class, that needs a reference to the component that created it and some of its public variables.
    Code (csharp):
    1.  
    2. public class Child_to_control
    3. {
    4. public ControllerA ThisController;
    5.  
    6.     public void creator()
    7.     {
    8.        if(ThisController != null){
    9.        Debug.Log (ThisController.gameObject.name);
    10.        }
    11.     }
    12. }
    13.  
    It never goes in the Debug, so ThisController is always null. If i try to access any public variable of ThisController also i get "NullReferenceException: Object reference not set to an instance of an object".

    Thanks
     
    Last edited: Oct 5, 2014
  2. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Assigning value to variable shouldn't produce error based on it's current value. But before we talk about value of Parentscipt variable better question is: what is Parentscipt variable? I don't see you declare any such variable anywahere in Parent class. If you want to create and instance of NEW_CHILD type and assigne some value to its field do this:
    Code (csharp):
    1.  
    2. NEW_CHILD Otherscipt = new NEW_CHILD();
    3. Otherscipt.parent = this;
    4.  
    and later in NEW_CHILD instance assign value to field of Parent isntance then do this:
    Code (csharp):
    1.  
    2. parent.Var = 5;
    3.  
    Now what does introducing another type of Child has to do with this? Would be better if you posted an actuall code that compiles and also format your code: http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
    nasos_333 likes this.
  3. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Parent : MonoBehaviour {
    5.     // variable
    6.     int var = 0;
    7.  
    8.     void Awake() {
    9.         // Assigns reference to child
    10.         Child.parentScript = this;
    11.  
    12.         // Changes variable value
    13.         Child.parentScript.var = 5;
    14.     }
    15.  
    16.     void Start() {
    17.         // Calls the static function
    18.         Child.Function();
    19.     }
    20.  
    21.     class Child : System.Object {
    22.  
    23.         public static Parent parentScript; // Parent Reference
    24.  
    25.         // Some static function
    26.         public static void Function() {
    27.             UnityEngine.Debug.Log("Var: " + parentScript.var);
    28.         }
    29.     }
    30. }
    31.  
    Not entirely sure I understand, but I think this is what you are looking for.
     
    nasos_333 and Pirs01 like this.
  4. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    He asked for assigning non-static fields so:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Parent : MonoBehaviour {
    5.     // variable
    6.     int var = 0;
    7.  
    8.     void Awake() {
    9.         // Assigns reference to child
    10.         //Child.parentScript = this;
    11.         Child child = new Child();
    12.         child.parentScript = this;
    13.         child.Function();
    14.     }
    15.  
    16.     // There's no need for Child being decalred inside Parent for this particular problem
    17.     class Child : Parent // that's what he ment I assume because of Parent / Child names
    18.     {
    19.  
    20.         public Parent parentScript; // Parent Reference
    21.  
    22.         // Some non-static function
    23.         public void Function() {
    24.             //UnityEngine.Debug.Log("Var: " + parentScript.var);
    25.             parentScript.var = 5; // and he want's to set some value back and forth between them I thnk
    26.         }
    27.     }
    28. }
    29.  
     
    Last edited: Oct 5, 2014
    nasos_333 likes this.
  5. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    Ah I misread. I don't think he wants to use inheritance though...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Parent : MonoBehaviour {
    5.     // variable
    6.     int var = 0;
    7.  
    8.     // Creates a new instance of the child.
    9.     Child child = new Child();
    10.  
    11.     void Awake() {
    12.         // Assigns reference to child
    13.         child.parentScript = this;
    14.  
    15.         // Changes variable value
    16.         child.parentScript.var = 5;
    17.     }
    18.  
    19.     void Start() {
    20.         // Calls the function
    21.         child.Function();
    22.     }
    23.  
    24.     class Child : System.Object {
    25.  
    26.         public Parent parentScript; // Parent Reference
    27.  
    28.         // Some function
    29.         public void Function() {
    30.             UnityEngine.Debug.Log("Var: " + parentScript.var);
    31.         }
    32.     }
    33. }
    34.  
     
    nasos_333 likes this.
  6. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    Thanks

    The lines

    1. NEW_CHILD Otherscipt = new NEW_CHILD();
    2. Otherscipt.parent = this;
    is what i currently do and it still gives me a null in the parent variable in the child function.

    Also i do not want to the child class to extend the parent, they are not connected

    And i suppose i would not want any static declaration inside the child either, due to the problems this could cause. The problem is that i want to avoid using static in general, since i may have multiple parent components.
     
  7. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Well PolymorphiK Games gave you plenty of example to work with. If still having problems then please post the code and tell us what the error message is exactly and what line (in posted code and not your file)
     
    nasos_333 and PolymorphiK Games like this.
  8. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    Well, I am surprised it even compiles. NEW_CHILD is not a class its a variable...I cannot say for instance:

    GameObject gObj = new GameObject();
    gObj otherObject = new gObj(); // this is impossible.

    I am not a UnityScript coder, but I doubt you can do that even if #pragmascrict was not present.
     
    nasos_333 likes this.
  9. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    You think it is safe to use static in the child class ?
    Yes, got a bit confused with the sample

    void Start(){
    NEW_CHILD= new Child();
    NEW_CHILD.Parentscipt = this;
    }

    This is what i use and still get null in the Parentscipt when i try to access from the child class.
     
  10. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    No no no. I was just making apoint. You don't put that in your code and expect to work especially since I wrote that based not on your actuall code you never posted but your messy psuedo code. Please post your entire code (relevant part).
     
    nasos_333 likes this.
  11. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    Here is the exact code

    The controller
    Code (csharp):
    1.  
    2.  
    3. public class ControllerA : MonoBehaviour {
    4.  
    5. private Child_to_control New_child;
    6.  
    7. void Start () {
    8.  
    9.      New_child = new Child_to_control();
    10.      New_child.ThisController = this;
    11.  
    12.     }
    13. }
    14.  
    The Child_to_control class, that needs a reference to the component that created it and some of its public variables.
    Code (csharp):
    1.  
    2. public class Child_to_control
    3. {
    4. public ControllerA ThisController;
    5.  
    6.     public void creator()
    7.     {
    8.        if(ThisController != null){
    9.        Debug.Log (ThisController.gameObject.name);
    10.        }
    11.     }
    12. }
    13.  
    It never goes in the Debug, so ThisController is always null. If i try to access any public variable of ThisController also i get "NullReferenceException: Object reference not set to an instance of an object".
     
  12. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    In this line:
    Code (csharp):
    1.  
    2. New_child = new Child_to_control();
    3.  
    the constructor of Child_to_control is called and the field ThisController is not yet set. Only next line (after constructor has finished executing) you set the field ThisController:
    Code (csharp):
    1.  
    2. New_child.ThisController = this;
    3.  
    You need to set the ThisController field before checking for it's value if you expect to find anything there for example like this:

    Code (csharp):
    1.  
    2. public class ControllerA : MonoBehaviour
    3. {
    4.   private Child_to_control New_child;
    5.  
    6.   void Start ()
    7.   {
    8.      New_child = new Child_to_control(this);
    9.      //New_child.ThisController = this;
    10.   }
    11. }
    12.  
    13. public class Child_to_control
    14. {
    15.   public ControllerA ThisController;
    16.  
    17.   public void construct(ControllerA contrA)
    18.   {
    19.      ThisController = contrA
    20.      if(ThisController != null){
    21.      Debug.Log (ThisController.gameObject.name);
    22.   }
    23. }
    24.  
    or like this:
    Code (csharp):
    1.  
    2. public class ControllerA : MonoBehaviour
    3. {
    4.   private Child_to_control New_child;
    5.  
    6.   void Start ()
    7.   {
    8.      New_child = new Child_to_control();
    9.      New_child.ThisController = this;
    10.      New_child.checkValue();
    11.   }
    12. }
    13.  
    14. public class Child_to_control
    15. {
    16.   public ControllerA ThisController;
    17.  
    18.   public void construct()
    19.   {
    20.    
    21.   }
    22.   public void checkValue()
    23.   {
    24.      if(ThisController != null){
    25.      Debug.Log (ThisController.gameObject.name);
    26.      }
    27.   }
    28. }
    29.  
     
    nasos_333 likes this.
  13. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Except, now I noticed your constructors are not contructors. There's a method called construct which is never called any where so it should be:
    Code (csharp):
    1.  
    2. public class ControllerA : MonoBehaviour
    3. {
    4.   private Child_to_control New_child;
    5.  
    6.   void Start ()
    7.   {
    8.      New_child = new Child_to_control(this);
    9.      //New_child.ThisController = this;
    10.   }
    11. }
    12.  
    13. public class Child_to_control
    14. {
    15.   public ControllerA ThisController;
    16.  
    17.   public Child_to_control(ControllerA contrA)
    18.   {
    19.      ThisController = contrA
    20.      if(ThisController != null){
    21.      Debug.Log (ThisController.gameObject.name);
    22.   }
    23. }
    24.  
    or like this:
    Code (csharp):
    1.  
    2. public class ControllerA : MonoBehaviour
    3. {
    4.   private Child_to_control New_child;
    5.  
    6.   void Start ()
    7.   {
    8.      New_child = new Child_to_control();
    9.      New_child.ThisController = this;
    10.      New_child.checkValue();
    11.   }
    12. }
    13.  
    14. public class Child_to_control
    15. {
    16.   public ControllerA ThisController;
    17.  
    18.   public Child_to_control()
    19.   {
    20.  
    21.   }
    22.   public void checkValue()
    23.   {
    24.      if(ThisController != null){
    25.      Debug.Log (ThisController.gameObject.name);
    26.      }
    27.   }
    28. }
    29.  
    You should read about constructors: http://msdn.microsoft.com/en-us/library/ace5hbzh.aspx
     
    nasos_333 likes this.
  14. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    I see, thanks a lot, this is probably the cause, i will try the new code asap :)
     
  15. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    Ok, it worked :), thanks a huge lot everyone for the help.
     
  16. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Why not just inherit and make the variables you need to access protected or public?
     
  17. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    You mean the Child class inherit from the parent ControllerA class ?