Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 alexanderameye · Jun 26, 2015 at 12:58 PM · variablebooleandoordifferentdoors

Private and public variables

Hey guys,

I have script that checks if you are near a door and if you are within a certain range, the script returns the variable 'InRange' true. This variable is a public variable in this script. Then I have another script that opens the door when you pressed E and 'InRange is true'. Now the problem is, if I have multiple doors in my scene and I am near door 1, InRange is returned true, but because I use this variable for every door, also door 2 and 3 think that I am near them while this is not true. So when I press 'E', all the doors open at once. How can I make it so that only the door I am really InRange of opens. So like how can I make a variable that has one name, but has a different values on the different doors (with each assigned an individual script to them) Thank you so so much!

This is the code for checking if you are near the door:

 //INSPECTOR SETTINGS
     public float Range = 5; // Within this radius the player is able to open/close the door.
     public bool InRange;
 
     //DEBUGGING
     public Color DebugRayColor = Color.red;
 
     //START
     void Start()
     {
 
     }
 
     //UPDATE
     void Update()
     {
         InRange = false;
 
         Ray ray = Camera.main.ViewportPointToRay (new Vector3 (0.5F, 0.5F, 0F)); // Set origin of ray to center of screen and direction of ray to cameraview.
         RaycastHit hit; // Variable reading information about the hit.
 
         if(Physics.Raycast (ray, out hit, Range)) // Casts a ray from the center of screen towards where the player is looking.
         {
             if(hit.collider.tag == "Door") // Change the tag you use for your doors here.
             {
                 InRange = true;
             }
         }


this is the code for rotating the door:

 //UPDATE
     void Update ()
     {
         //Access InRange variable from raycasting script
         GameObject Player = GameObject.Find("Player");
         RayCasting raycasting = Player.GetComponent<RayCasting>();
 
         if (raycasting.InRange == true) {
             InRange = true;
         }
 
         else InRange = false;
 
         //DEBUGGING
         //print (InRange);
 
         if (Input.GetKey(KeyCode.E) && InRange == true) {
             Openable = true;
         }
 
         if (Openable)
         {
     //Rotate();
 
             }
         }
     }


Comment
Add comment · Show 3
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 Hellium · Jun 26, 2015 at 01:10 PM 1
Share

Please, provide some code. It will be much easier to help you and tell you what's wrong.

avatar image alexanderameye · Jun 26, 2015 at 01:15 PM 0
Share

allright done

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Hellium · Jun 26, 2015 at 01:22 PM

Your code seems to be a very big mess ...

A big part of the logic should be in the player, and only in the player :

 void Update()
 {
     if (Input.GetKeyUp(KeyCode.E) )
     {
         Ray ray = Camera.main.ViewportPointToRay (new Vector3 (0.5F, 0.5F, 0F)); // Set origin of ray to center of screen and direction of ray to cameraview.
         RaycastHit hit; // Variable reading information about the hit.
 
         if(Physics.Raycast (ray, out hit, Range) && hit.collider.tag == "Door")  // Casts a ray from the center of screen towards where the player is looking.
         {
             Door door = hit.GameObject.GetComponent<Door>().Open() ;
         }
     }
 }

Then, in the script attached to your doors, define a function called Open() to rotate the door.

Comment
Add comment · Show 13 · 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 Wolfdog · Jun 26, 2015 at 01:40 PM 1
Share

+1 - I find this as the best approach. Even better than $$anonymous$$e.

avatar image alexanderameye · Jun 26, 2015 at 01:41 PM 0
Share

Cool I'll take a look at it when I have some more time, thank you both!!

avatar image alexanderameye · Jun 27, 2015 at 10:17 AM 0
Share

I didn't test this yet, but if I have $$anonymous$$ultiple doors in the game level and they all have the tag door, won't this open all of the doors? Or does it only count for the door that is hit because of this line of code:

  Door door = hit.GameObject.GetComponent<Door>().Open() ;
avatar image Wolfdog · Jun 27, 2015 at 11:37 AM 1
Share

@alexanderameye it works. But remember, you have to have a script on all of your doors named Door and a public void Open() {//Rotate function} inside of it.

avatar image Hellium · Jun 27, 2015 at 10:50 PM 1
Share

Yes, the fonction I wrote you in the player script and an Open() function in the script attached to each individual door.

If the open function is defined in another script, just call the function of that script in the Open function of the Door script

Show more comments
avatar image
2

Answer by Wolfdog · Jun 26, 2015 at 01:33 PM

This is how I would do it. I wouldn't use rays, but trigger colliders.

 // Assuming this is the player script
      //UPDATE
      void Update()
      {
          // nothing needs to be done here. 
          // just remember to have a trigger collider on your player with a radius of 5
          // and also have a rigidbody either on the player or on all of the doors
      }
      
      void OnTriggerEnter (Collider c) {
          if (c.transform.tag == "Door") {
              c.transform.GetComponent <YourDoorScriptHere> ().inRange ();
          }
      }
      
      void OnTriggerExit (Collider c) {
          if (c.transform.tag == "Door") {
              c.transform.GetComponent <YourDoorScriptHere> ().outOfRange ();
          }
      }
 
 // Assuming this is the door script     
 //UPDATE

      private bool Openable = false;

      void Update ()
      {
          if (Input.GetKey(KeyCode.E) && Openable) {
              Rotate(); // open door script
          }
      }
      public void inRange  () {
             Openable = true;
      }
          
      public void outOfRange () {
          Openable = false;
      }

(this is untested. Leave a comment if any problems arise)

This approach is better than using rays, because it isn't as CPU intensive as casting rays every frame.

Comment
Add comment · Show 2 · 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 Hellium · Jun 26, 2015 at 01:37 PM 2
Share

It's even less CPU intensive to draw a ray only when the player has pressed the action key.

avatar image Wolfdog · Jun 26, 2015 at 01:41 PM 1
Share

....Agreed

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Toggle Boolean Function 2 Answers

Script not responding to public variable change 1 Answer

Scene Change OnCollision Not Working 2 Answers

How to stop a boolean from going back to false 1 Answer

Access a variable from another script in update function 1 Answer


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