Search Unity

problem with referencing/editing a script on the same object

Discussion in 'Scripting' started by Cruor, Feb 13, 2018.

  1. Cruor

    Cruor

    Joined:
    Nov 24, 2012
    Posts:
    4
    I've been tinkering with my code and was able to bring in variables from script1 on object1 into script2 on object2, i also managed to update the variables on script1, but if i put script1 and script2 on object1, im not able to update the variables in script1. I don't get any errors (red or yellow) in the console. I added debug.log to the editnumber/bool functions, and i can get numberRef and boolRef to update once, but not more than that. Any help on his issue would be appreciated, and let me know if i should provide more info.

    Code (CSharp):
    1. //Script1
    2. //so far script1 just makes these variables
    3.     public int obj1number = 10;
    4.     public bool obj1bool = false;
    5.  
    6.  
    7. //Script2
    8.     public GameObject obj1;
    9.     private Script1 refScript;
    10.     private int numberRef;
    11.     private bool boolRef;
    12.  
    13. void Start()
    14. {
    15. InitializeVariables();
    16. }
    17.  
    18. void InitializeVariables()
    19.     {
    20.         refScript = obj1.GetComponent<Script1>();
    21.         numberRef = refScript.obj1number;
    22.         boolRef = refScript.obj1bool;
    23.         Debug.Log(numberRef);
    24.         Debug.Log(boolRef);
    25.     }
    26. void Update()
    27. {
    28.         EditNumber();
    29.         EditBool();
    30.  
    31.         refScript.obj1number = numberRef;
    32.         refScript.obj1bool = boolRef;
    33. }
    34. //EditNumber and EditBool are basically the same, input changes variable
    35. void EditNumber()
    36. {
    37. if(Input.GetKeyDown(KeyCode))
    38. {
    39. numberRef +=1;
    40. }
    thanks to anyone that can help in advance,
     
    Last edited: Feb 13, 2018
  2. fetish

    fetish

    Joined:
    Aug 25, 2015
    Posts:
    73
    The problem is that Obj1 is never set to anything - it's just an empty, virtual gameObject that's never assigned and remains null.

    assuming script1 and script2 are on the same game object, you could simply call GetComponent<Script1>().obj1bool = <whatever>
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This line doesn't make sense to me:
    Code (csharp):
    1. if(Input.GetKeyDown(KeyCode))
    You need to specify a KeyCode, such as "KeyCode.Space", otherwise what key press is it looking for?
    https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html

    Also, if the two scripts are on the same GameObject, you don't need a reference to that GameObject. You can just call GetComponent<Script1>() and it defaults to the same GameObject that the script is running on. Otherwise the way you have it setup is you need to set the reference to obj1 in the inspector manually. If you didn't though then you would be hitting a null reference error when you hit play, so I'm assuming you've set that correctly.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'd add a small suggestion: update your number and bool if they are modified. If the keycode value is matched, do the update then and there, and remove the assignment that happens every update.

    Hopefully some of the previous answers help you get it sorted.
     
  5. Cruor

    Cruor

    Joined:
    Nov 24, 2012
    Posts:
    4
    Thank you for the responses,
    i didn't copy the code verbatim, so it i messed up a little with the keycode line, it was supposed to be KeyCode.LeftShift; obj1 is public, so i dragged the gameobject to the script in the inspector, and thanks for that last bit, i dont think i planned on keeping the assignment in the update function, i have notes for updating the origin variable with its own function, but it could be simpler to update it with the key press. I'll try out the changes when i get home from work.