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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bool Trigger Always = true problem

Discussion in 'Scripting' started by spartaboy300, Aug 6, 2015.

  1. spartaboy300

    spartaboy300

    Joined:
    Jun 24, 2014
    Posts:
    16
    Basically what's supposed to happen is the player can walk up to a door of a building in a town (where a collider is) and press 'E' to change to that scene. But instead of the player needing to be in the collision trigger, they can press e from anywhere and transition scenes. So I just assumed that "innTrigger" is automatically being assigned as true for some reason. I appreciate all the help!!! :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class SceneChange : MonoBehaviour {
    6.    
    7.     public Text pressE;
    8.     public Collider Player;
    9.     public string Scene;
    10.     public bool innTrigger;
    11.  
    12.     void Start ()
    13.     {
    14.         pressE.text = "";
    15.         innTrigger = false;
    16.     }
    17.  
    18.  
    19.     void OnTriggerEnter (Collider Player)
    20.     {
    21.         innTrigger = true;
    22.         pressE.text = "Press 'E' to Enter";
    23.     }
    24.  
    25.     void Update ()
    26.     {
    27.         if (innTrigger = true)
    28.         {
    29.             if (Input.GetKeyDown ("e"))
    30.             {
    31.                 Application.LoadLevel (Scene);
    32.             }
    33.         }
    34.     }
    35.  
    36.     void OnTriggerExit (Collider Player)
    37.     {
    38.         innTrigger = false;
    39.         pressE.text = "";
    40.     }
    41. }
    42.  
     
  2. minionnz

    minionnz

    Joined:
    Jan 29, 2013
    Posts:
    391
    if (innTrigger = true) should be if (innTrigger == true)
     
    angrypenguin, Ryiah and zombiegorilla like this.
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,987
    Line 27.
    You have "=" which assigns true. You need "==" which compares. It will always return true the way you have it now.
     
    angrypenguin, Ryiah and minionnz like this.
  4. calmcarrots

    calmcarrots

    Joined:
    Mar 7, 2014
    Posts:
    654
    Im going to milk this even further.

    Change line 27 to
    Code (csharp):
    1. if(innTrigger == true)
    It is crucial to have TWO (2) equal signs!!!
     
    minionnz likes this.
  5. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    No no no, just use

    Code (CSharp):
    1. if(innTrigger)
     
  6. spartaboy300

    spartaboy300

    Joined:
    Jun 24, 2014
    Posts:
    16
    Thank you for the answers!
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just use the scripting forum next time. :rolleyes:
     
  8. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    At least he used code tags.
     
    GibTreaty, calmcarrots and Kiwasi like this.