- Home /
Change bool in only one GameObject
I have in my scene two doors, in both doors i have the script, everything for the doors works fine except the bool "isOpen". When i open one door it changes for both doors. Is there a way to change the bool individually for one door and not both?
Here's my code, and bear in mind i'm fairly new to Unity.
 public class Door : MonoBehaviour {
 
 //Distance to open variables 
 public float distanceToOpen = 10; private float distance;
 
 //If door is open 
 public bool isOpen; private Door door;
 
 //Delay to open/close door 
 public float delayTime = 2; private float delay = 2;
 
 //The player 
 private GameObject player; private Ray ray;
 
 //Audio 
 public AudioClip[] doorSounds; public float audioPitch; public float audioVolume;
 
 void Start () {
 
   player = GameObject.FindGameObjectWithTag ("Player");
   delay = delayTime;
 }
 
 void Update () {
 
   distance = Vector3.Distance(transform.position, player.transform.position);
   ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
   RaycastHit hit;
   
       
  
   if (distance < distanceToOpen && Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Door") {
  
       //door = hit.collider.gameObject.transform.parent.GetComponent<Door>();
  
           door = hit.collider.gameObject.transform.parent.GetComponent<Door>();
           isOpen = door.isOpen;
  
  
       //To open door
       if (Input.GetButtonDown("Interact") & isOpen == false && delay <= 0) {
           hit.collider.gameObject.transform.parent.animation.Play("DoorOpening");
           isOpen = true;
           door.audio.pitch = audioPitch;
           door.audio.volume = audioVolume;
           door.audio.clip = doorSounds[0];
           door.audio.Play();
           delay = delayTime;
           }
  
       //To close door
       if (Input.GetButtonDown("Interact") & isOpen == true && delay <= 0) {
           hit.collider.gameObject.transform.parent.animation.Play("DoorClosing");
           isOpen = false;
           door.audio.pitch = audioPitch;
           door.audio.volume = audioVolume;
           door.audio.clip = doorSounds[1];
           door.audio.Play();
           delay = delayTime;
       }
  
       //Subtract time from the delay
       delay = delay - Time.deltaTime;
   }
 }
Since isOpen is not static each door has its own boolean. When you open one (animation) is the other one opening too? How do you know both booleans get changed? You're code looks as if that's impossible.
Answer by NoseKills · Feb 28, 2015 at 09:21 AM
You have this script on all doors. So when you do ...
 void Update () {
    // every door with this script checks how far it is from the player 
    distance = Vector3.Distance(transform.position, player.transform.position);
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   
    RaycastHit hit;
   // every door that was close enough to player when something with tag "Door" was clicked, goes into this if
    if (distance < distanceToOpen && Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Door") {
            // every door script that got this far changes their isOpen to match the cliked door's isOpen
            door = hit.collider.gameObject.transform.parent.GetComponent<Door>();
            isOpen = door.isOpen;
So it would seem that perhaps your distanceToOpen is too large if really multiple doors go inside the if when you click a door. You could make sure by debugging:
 if (distance < distanceToOpen && Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Door") {
   Debug.Log("Door in range");
and see on how many doors that triggers with one click.
Also you would probably want to check that you change isOpen only in the script that's running on the clicked door. Like
 if (door == this) { isOpen = door.isOpen; //...do "Interact" stuff }
Answer by Kiwasi · Feb 28, 2015 at 09:23 AM
I assume this script is attached to the door?
In that case you are doing a raycast on both doors in your update function. Thus both doors change. If you had a hundred doors this multiple raycasting would cause noticeable performance spikes.
The best way to handle this is to do your raycasting on a separate script. Typically one attached to the player. That way you only touch the door of interest. And you only calculate the ray cast once.
So i tried the raycast co$$anonymous$$g from the player ins$$anonymous$$d and having a if statement to check if isOpen = true/false, and idk what i screwed up cause i keep getting NullReferenceException errors.
Script on Door public class Door : $$anonymous$$onoBehaviour {
 //Distance to open variables
 public float distanceToOpen = 10;
 private float distance;
 //If door is open
 public bool isOpen; 
 private Door door;
 //The player
 private GameObject player;
 private Ray ray;
 //Audio
 public AudioClip[] doorSounds;
 public float audio$$anonymous$$ch;
 public float audioVolume;
 void Start () {
     
     
 }
 
 void Update () {
  
         //To open door
     if (Input.GetButtonDown("Interact") & isOpen == true) {
             animation.Play("DoorOpening");
             audio.pitch = audio$$anonymous$$ch;
             audio.volume = audioVolume;
             audio.clip = doorSounds[0];
             audio.Play();
             }
         //To close door
     if (Input.GetButtonDown("Interact") & isOpen == false) {
         animation.Play("DoorClosing");
         audio.pitch = audio$$anonymous$$ch;
         audio.volume = audioVolume;
         audio.clip = doorSounds[1];
         audio.Play();
         }
     
     } 
 }
Script on Player
public class DoorPlayer : $$anonymous$$onoBehaviour {
 //Distance to open variables
 public float distanceToOpen = 10;
 private float distance;
 
 //If door is open
 public bool isOpen; 
 private Door door;
 private Animation doorAnim;
 
 //Delay to open/close door
 public float delayTime = 2;
 private float delay = 2;
 
 //The player
 private Vector3 player;
 private Ray ray;
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     distance = Vector3.Distance(transform.position, hit.transform.position);
     
     
     if (distance < distanceToOpen && Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Door") {
         door = hit.collider.gameObject.transform.parent.GetComponent<Door>();
         doorAnim = hit.collider.gameObject.transform.parent.GetComponent<Animation>();
         isOpen = door.isOpen;
         //To open door
         if (Input.GetButtonDown("Interact") & isOpen == false) {
             doorAnim.animation.Play("DoorOpening");
             isOpen = true;
         }
         
         //To close door
         if (Input.GetButtonDown("Interact") & isOpen == true) {
             doorAnim.animation.Play("DoorClosing");
             isOpen = false;
         }
     }
 }
}
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                