Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jaimyberlemon1999 · Feb 08, 2018 at 08:25 PM · unity 5scripting problemprefab

Giving different values to cloned prefabs with the same script.

Hey there, I am creating a game with a friend, but I realised there is a bug in my code. I am trying to create a script that opens and closes doors with an animation. I use a bool called 'open' to find out if the door is open or closed, but everytime I open/close a door, the bool value changes for every door with the same script. Any idea how I can fix this?

My code:

 public class DoorController : MonoBehaviour {
 
     private RaycastHit hit;
     private GameObject door;
     public  bool open;
     private bool cooldown;
     private bool loopBool;
     public GameObject pointer;
     public AudioClip openSound;
     public AudioClip closeSound;
 
 
 
     // Use this for initialization
     void Start () {
         open = false;
         cooldown = false;
         loopBool = true;
         door = this.gameObject;
     }
 
     private IEnumerator Open(){
         while (loopBool) {
             cooldown = true;
             yield return new WaitForSeconds (1);
             open = false;
             cooldown = false;
             break;
         }            
     }
 
     private IEnumerator Close(){
         while (loopBool) {
             cooldown = true;
             yield return new WaitForSeconds (1);
             open = true;
             cooldown = false;
             break;
         }            
     }
 
     // Update is called once per frame
     void Update () {
         Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1.5f);
 
         if (hit.collider != null && hit.collider.tag == "OpenableDoor") {
             //door = hit.collider.gameObject;
             pointer.SetActive (true);
             if (Input.GetKey (KeyCode.Mouse0)) {
                 if (cooldown == false) {
                     if (open) {
                         this.door.GetComponent<Animation> ().Play ("DoorClose");
                         AudioSource.PlayClipAtPoint(closeSound, transform.position);
                         StartCoroutine (Open ());
                     }
                     if (open == false) {
                         this.door.GetComponent<Animation> ().Play ("DoorOpen");
                         AudioSource.PlayClipAtPoint(openSound, transform.position);
                         StartCoroutine (Close ());
                     }
                 }
             }
         } else {
             pointer.SetActive (false);
         }
     }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by yummy81 · Feb 08, 2018 at 10:41 PM

So, let's assume that there are four doors at the scene. Each of them has its own DoorController script attached. Therefore, each door has its own bool and casts its own ray. So, we have four doors, four scripts, four bools, and four raycasts. None of the raycasts distinguish between door#1, door#2, door#3 and door#4. For raycasts, all the doors are the same. For raycasts, all that matters is whether the object that was hit has "OpenableDoor" tag. Each ray starts at the same position and has the same direction and length. So, when all four rays hit one of the four doors (no matter which one) then all four bools are adjusted. The solution to this would be to get rid of those three unnecessary raycasts and leave just one. In order to do that you have to create the script for raycasting, let's name it DoorController, and the script DoorScript which will be attached to each door. Create new gameobject and drop the DoorController script on it and then drop DoorScript on each door gameobject. The following code is just a hypothetical one, but as you know now where the problem lies, it's just a matter of tinkering with it to make it work as you want. Code for DoorController script:

 public class DoorController : MonoBehaviour 
 { 
     private RaycastHit hit;
     public GameObject pointer;
     
      void Update () 
      {
         Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1.5f);
  
          if (hit.collider != null && hit.collider.tag == "OpenableDoor") 
          {
              pointer.SetActive (true);
              if (Input.GetKey (KeyCode.Mouse0)) 
              {    
              // this piece of code was added by me
              
                 DoorScript d = hit.collider.GetComponent<DoorScript>();
                 if (d!=null)
                 {
                     d.Foo();
                 }
                 
             // that's where my code ends
              }
          } 
          else 
          {
              pointer.SetActive (false);
          }
     }
 }
 
 

Code for DoorScript. I added here new public method Foo and removed Update with raycasts:

  public class DoorScript: MonoBehaviour 
  {
      private GameObject door;
      public  bool open;
      private bool cooldown;
      private bool loopBool;
      public AudioClip openSound;
      public AudioClip closeSound;
  
  
  
      // Use this for initialization
      void Start () {
          open = false;
          cooldown = false;
          loopBool = true;
          door = this.gameObject;
      }
  
      private IEnumerator Open(){
          while (loopBool) {
              cooldown = true;
              yield return new WaitForSeconds (1);
              open = false;
              cooldown = false;
              break;
          }            
      }
  
      private IEnumerator Close(){
          while (loopBool) {
              cooldown = true;
              yield return new WaitForSeconds (1);
              open = true;
              cooldown = false;
              break;
          }            
      }
      
          public void Foo()
     {
         if (cooldown == false) {
              if (open) {
                  this.door.GetComponent<Animation> ().Play ("DoorClose");
                  AudioSource.PlayClipAtPoint(closeSound, transform.position);
                  StartCoroutine (Open ());
              }
              if (open == false) {
                  this.door.GetComponent<Animation> ().Play ("DoorOpen");
                  AudioSource.PlayClipAtPoint(openSound, transform.position);
                  StartCoroutine (Close ());
              }
          }    
     }
 }
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 jaimyberlemon1999 · Feb 09, 2018 at 04:51 PM 0
Share

Sorry for the late reaction, but thank you! This really helped me!

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

256 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 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 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 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 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 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 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 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 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 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 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 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

how to instantiate a prefabs that move upward on update function and get the transform position of that moving prefab 0 Answers

How do I enable/disable components in a prefab from another script? 1 Answer

Check if collision between the model and two different objects is happening at the same time 0 Answers

ERROR CS0649 help please guys 0 Answers

Getting wrong value of variable 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