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

Code

Discussion in 'Scripting' started by ellenblomw, Jul 29, 2019.

  1. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hi all,

    I have two buttons controlling two bools and another gameobject reading these bools and if they are true I want stuff to happen. If I press the "next button" everything works! AKA Debug.Log("Next is true") shows up in console.

    But if I press the "last button" the Debug.Log("Last is true") dont show up even though I can see in the inspector that the button was pressed and the bool named last keeps being true.

    Code on buttons
    Code (CSharp):
    1.     void OnMouseDown()
    2.     {
    3.         //If player press the "next button" where nextGameObject has bool = true in inspector, then the public bool next should become true
    4.         if (nextGameObject == true)
    5.         {
    6.             next = true;
    7.         }
    8.         //If player press the "last button" where lastGameObject has bool = true in inspector, then the public bool last should become true
    9.         if (lastGameObject == true)
    10.         {
    11.             last = true;
    12.         }
    13.     }
    I then set lastGameObject true on last button in inspector, and nextGameObject true for next button in inspector.

    Code on another gameobject where I eventually want stuff to happen to its children when I know the code is working:

    Code (CSharp):
    1.  
    2.     private switchCamList switchCamList;
    3.  
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         switchCamList = FindObjectOfType<switchCamList>();
    8.     }
    9.  
    10.     private void Update()
    11.     {
    12.         //If bool last from switchCamList is true then do this THIS DOES NOT EXECUTE
    13.         if (switchCamList.last == true)
    14.         {
    15.             Debug.Log("Last is true");
    16.             switchCamList.last = false;
    17.         }
    18.         //If bool next from switchCamList is true then do this THIS WORKS
    19.         if (switchCamList.next == true)
    20.         {
    21.             Debug.Log("Next is true");
    22.             switchCamList.next = false;
    23.         }
    24.     }
    25. }
    26.  
    What am I missing here? Why is only the code for the next button working when the code has the same logic on the last button and the last bool is true?
     
  2. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Oh never mind, I of course had to reference each buttons script since I had it on two objects with different settings

    if (lastBut.GetComponent<switchCamList>().last == true)