• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 CUTCUTCUT · Jul 29, 2015 at 06:20 PM · animationgameobjectarraydoor

Door object array

I tried opening doors with GameObject Arrays and what happend is when I tried open a door it didnt open but instead another door opened which is also in the array and I wasnt even trying to open it.

I think this is a stupid question, but I dont know how to get the door open which I am aiming at.

 #pragma strict
 
 private var isOpen : boolean = false;
 
 var doorOpenSound : AudioClip[];
 var doorCloseSound : AudioClip[];
 
 private var object_door : GameObject[];
 var object_door_tagged : GameObject[];
 
 var i:int=0;
 var rayLenght = 10;
 
 
 function Start(){
 object_door = GameObject.FindGameObjectsWithTag("DoorDefault");
 
   for(object_door in object_door_tagged){
 
     Debug.Log(object_door_tagged[i]);
 
   }
 }
 
 
 function Update(){
 
  var hit : RaycastHit;
  var fwd = transform.TransformDirection(Vector3.forward);
  if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
   if(hit.collider.gameObject.tag == "DoorDefault"){
    Debug.Log("Door hit");
    if(Input.GetButtonDown("Interaction") && isOpen == false){
     object_door_tagged[i].animation.Play("door_wood_open");
     
        audio.PlayOneShot(doorOpenSound[Random.Range (0, doorOpenSound.length)]);
        audio.Play();
     
      isOpen = true;
    }
    else if(Input.GetButtonDown("Interaction") && isOpen == true){
     object_door_tagged[i].animation.Play("door_wood_close");
     
        audio.PlayOneShot(doorCloseSound[Random.Range (0, doorCloseSound.length)]);
        audio.Play();
 
     isOpen = false;
    }
   
   }
 
  }
 
 }

Also when I click on the door I add all the objects from that door into an array, I think its because Im calling that array every frame, but i dont know if i can do it in start function or is there a way for update function to not add the objects that are already in the array.

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 Positive7 · Jul 29, 2015 at 07:30 PM 1
Share

Assuming that you don't see all the door you could just use hit.collider.GetComponent().Play ("door_wood_open");

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Lahzar · Jul 29, 2015 at 06:58 PM

You shouldn't do that like this! You should use a fairly advanced but simple concept called an interface!

It works like this: You raycast (like you have done) forwards, but instead of checking the tag you should get every single script on this object. Do that like this:

 foreach(var script in hit.gameObject.GetComponents<MonoBehaviour>()) {
     //Do something to every script attached to hit object
 }

Now you need to check if this is indeed a door. But first we need to decide what is a door and not. Create two new scripts. One called Door and one called IDoor. IDoor should be an interface. An interface should contain any variables that are required for a door. Long story short, write this in your IDoor:

 public interface IDoor { //Replace the "class" with interface and remove the " : MonoBehaviour"
     void ChangeDoorState(); //Any script 'implementing' this interface MUST contain this function!
 }

And then write something like this for your door: public class Door : MonoBehaviour, IDoor { //You need to implement the IDoor interface by adding , IDoor behind MonoBehaviour!

     public void IDoor.ChangeDoorState() {  //Since Door implements IDoor: Door must have a void called ChangeDoorState that is public!
         //If door is closed, open
         //Else, close
     }
 }

Why you should do this might not seem very clear right now, but what you now can do inside your raycast and foreach statement is this:

 foreach(var script in hit.gameObject.GetComponents<MonoBehaviour>()) {
     //Do this for every script attached to hit.gameObject
     if(script is IDoor) { //If the script were checking implements IDoor
         (script as IDoor).ChangeDoorState(); //Since it is an IDoor, we know it must have a function called ChangeDoorState!
     }
 }

I havent tested this code, but as long as you attach the door script to a door this should work very nicely!

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 Lahzar · Jul 29, 2015 at 07:04 PM 0
Share

Also, you should raycast inside your input, not opposite!

Also, thing about interfaces, is that they can be used much better! Instead of having an IDoor, you could have an IInteract, and then if the object you click is IInteract, then you could call its Interact(), which in the doors case would open/close the door!

avatar image
1

Answer by Positive7 · Jul 29, 2015 at 07:41 PM

  #pragma strict
  
  private var isOpen : boolean = false;
  
  var doorOpenSound : AudioClip;
  var doorCloseSound : AudioClip;
 
  var rayLenght = 10;
 
  function Update(){
  
   var hit : RaycastHit;
   var fwd = transform.TransformDirection(Vector3.forward);
     if(Input.GetButtonDown("Interaction") && isOpen == false){
         if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
             if(hit.collider.gameObject.tag == "DoorDefault"){
                  hit.collider.GetComponent<Animation>().Play("door_wood_open");
     
                 audio.PlayOneShot(doorOpenSound[Random.Range (0, doorOpenSound.length)]);
                 audio.Play();
                   isOpen = true;
             }
         }
     }else if(Input.GetButtonDown("Interaction") && isOpen == false){
         if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
             if(hit.collider.gameObject.tag == "DoorDefault"){
             hit.collider.GetComponent<Animation>().Play("door_wood_close");
     
                 audio.PlayOneShot(doorCloseSound[Random.Range (0, doorCloseSound.length)]);
                 audio.Play();
                   isOpen = false;
             }
         }
     }
 }
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 CUTCUTCUT · Jul 31, 2015 at 10:28 AM 0
Share

I did this, and it came up with a problem. When i open a door and try to open another door it just plays the animation of closing the door, and debug writes in console that door has been closed. What can i do to only open a door if its closed, and if its open to close it.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotating Door 2 Answers

How to I play an animation using the Animation Element Array via Script? 0 Answers

2D GameObject Array to 2D List 1 Answer

Created an Open Close Animation, but script wont activate both doors? 1 Answer

Make two animations play when game object clicked 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges