Search Unity

Class Functions don't seem to be updating class variables

Discussion in 'Scripting' started by Drooby100, Jul 30, 2018.

  1. Drooby100

    Drooby100

    Joined:
    Jul 30, 2018
    Posts:
    4
    The SetNode function works as it should, drawing a line between the startNode and endNode objects but then when after I set the start node object to null in the update function, in the SetNode function the startNode variable still has it's value. It's like the startNode variable in the SetNode function is acting like a local variable instead of the class wide variable but I don't know why.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ArrowPlacer : MonoBehaviour {
    6.    
    7.     private GameObject startNode;
    8.     private GameObject endNode;
    9.     void Start () {
    10.        
    11.     }
    12.     void Update () {
    13.         if (Input.GetMouseButtonDown (1)) {
    14.             startNode = null;
    15.         }
    16.    
    17.     }
    18.  
    19.     public void SetNode(GameObject state){
    20.         if (startNode==null) {
    21.             startNode = state;
    22.  
    23.         } else {
    24.             endNode = state;
    25.             LineRenderer line = startNode.GetComponent<LineRenderer> ();
    26.             line.SetPosition (0, startNode.transform.position);
    27.             line.SetPosition (1, endNode.transform.position);
    28.             startNode = null;
    29.             endNode = null;
    30.         }
    31.     }
    32. }
    33.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    How are you calling SetNode? Because even if you set startNode to null, it's possible SetNode is being called and changing the value.

    I would put some debug messages in there and make sure your if statement in Update is called properly and then put some in SetNode to see where and when the parts are called.
     
  3. Drooby100

    Drooby100

    Joined:
    Jul 30, 2018
    Posts:
    4
    SetNode is called when an object is clicked on. That object then calls the function with the parameter being the gameobject. I have put debug messages and breaks into to see what parts are being called and also set it up as one of those variables with accessor methods. Those methods only get used when the startNode object is accessed withing update, not when accessed from SetNode. ( I checked this by putting a debug message in the set method)
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Do note that
    SetNode
    is public. This means it needs to take care of its internal state. This, in turn, means it should really validate all inputs. If you read my previous reply (in another thread), you may recall I mentioned to add a null check at the head of that method. If state gets handed in as null, it can mess with your internal logic.

    Now I'm not saying whether that is the cause of your problem here or not. It is just something that a public method should do to ensure internal integrity remains intact. :)