Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bool value doesn't seem to changing

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

  1. Drooby100

    Drooby100

    Joined:
    Jul 30, 2018
    Posts:
    4
    begin should start as true, and it sort of does, however when the SetNode function is entered, begin is false even though when using the debug tools the value is true when in the Update function

    Basically, the code should draw a line between 2 game objects. The SetNode function is called when an object is clicked and a reference to that object is the parameter of the SetNode function. However, when the SetNode function is entered, the begin variable is false for whatever reason so causes an error because the start variable has nothing in it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ArrowPlacer : MonoBehaviour {
    6.     private bool begin=true;
    7.     GameObject start;
    8.     GameObject end;
    9.     void Start () {
    10.        
    11.         begin = true;
    12.     }
    13.     void Update () {
    14.        
    15.     }
    16.     public void SetNode(GameObject state){
    17.         if (begin) {
    18.             start = state;
    19.             begin = false;
    20.         } else {
    21.             end = state;
    22.             LineRenderer line = start.GetComponent<LineRenderer> ();
    23.             line.SetPosition (0, start.transform.position);
    24.             line.SetPosition (1, end.transform.position);
    25.             begin = true;
    26.         }
    27.     }
    28. }
    29.  
    Thanks for the help!
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Are you certain that SetNode wasn't called with a null value? I would recommend :
    1. Change the variable name "start" on line 7. It is too similar to the Unity function name "Start". How about "startNode" instead?
    2. Remove the begin variable completely.
    3. In SetNode, change the if to be:
    Code (CSharp):
    1. if(state == null)
    2.    return;
    3.  
    4. if(startNode == null)
    5. <rest of your code here>
    Does that help?
     
  3. Drooby100

    Drooby100

    Joined:
    Jul 30, 2018
    Posts:
    4
    Yeah, that seems to have worked, thanks!
     
    Doug_B likes this.