Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kyle_L · Feb 28, 2015 at 09:08 AM · c#gameobjectchangedoorbool

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;
   }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Feb 28, 2015 at 09:20 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kyle_L · Feb 28, 2015 at 03:56 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Any way to change transform.position of an indefinite number of gameobjects on FixedUpdate? 2 Answers

C# Change Script Help 1 Answer

Change gameobject tag when player collides with another gameobject 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges