• 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 itsboshytime · Jan 09, 2014 at 11:40 PM · c#instances

Object instances

Hello. I am working on an open/close system for chests and the problem i found was that if i have multiple chests. Opening/closing one triggers all of them. Any way to bypass it? I have a chest prefab that i manually added to the sene multiple times.

Here is my chest script attached to them if it helps.

//needs cleanup

using UnityEngine; using System.Collections; using System.Collections.Generic;

[RequireComponent(typeof(BoxCollider))] [RequireComponent(typeof(AudioSource))]

public class Chest : MonoBehaviour {

 GameObject cameraGO;

 public enum State {
     open,
     close,
     between
 }

 public State state;
 public string key;
 public AudioClip openSound;
 public AudioClip closeSound;
 public GameObject[] components;
 private Color[] _defaultColors;

 public GameObject openEffect;

 public Shader defaultShader;
 public Shader outlineShader;

 public float outlineSize = 0.01f;
 public Color outlineColor = Color.black;

 public GameObject gui;
 public float maxRange = 3;
 private GameObject _player;





 // Use this for initialization
 void Start () {

     defaultShader = Shader.Find("Diffuse");
     outlineShader = Shader.Find("Outlined/Silhouetted Diffuse");

     cameraGO=GameObject.FindGameObjectWithTag("MainCamera");
     state = Chest.State.close;
     openEffect.SetActive(false);

     _defaultColors =  new Color[components.Length];

     if (components.Length > 0)
                     for (int i = 0; i< _defaultColors.Length; i++) {
         _defaultColors[i] = components[i].renderer.material.GetColor("_Color");
                     }
 
 }
 
 // Update is called once per frame
 void Update () {

     if(Input.GetKeyDown(key)){
         chestInteraction(3);
     }

     visibleContainer (10);

     }


 public void chestInteraction(float range){

     Ray ray = new Ray(cameraGO.transform.position, cameraGO.transform.forward);
     RaycastHit hit;
     
     if (Physics.Raycast (ray, out hit)) {
         
         if(hit.distance<range){ 

             if(hit.transform.tag == "Chest"){
             Debug.DrawLine(ray.origin,ray.origin+( ray.direction*hit.distance),Color.red,2);
             //Debug.Log("Chest Found");

             switch(state){

                 case State.open:
                     state = Chest.State.between;
                     StartCoroutine("CloseContainer");
                     break;

                 case State.close:
                     state = Chest.State.between;
                     StartCoroutine("OpenContainer");
                     break;

                 }
                 
                 }
             }
             
         }
     }


 public IEnumerator OpenContainer(){
     Debug.Log("Chest Opened");
     openEffect.SetActive (true);
     audio.PlayOneShot (openSound);
     yield return new WaitForSeconds(3);

     state = Chest.State.open;

 }


 public IEnumerator CloseContainer(){
     Debug.Log("Chest Closed");
     openEffect.SetActive (false);
     audio.PlayOneShot (closeSound);
     yield return new WaitForSeconds(3);

     state = Chest.State.close;

     }


 public void visibleContainer(float range){

     
     Ray ray = new Ray(cameraGO.transform.position, cameraGO.transform.forward);
     RaycastHit hit;
     
     if (Physics.Raycast (ray, out hit) && (hit.distance < range) && (hit.transform.tag == "Chest")  ) {

                                     Debug.DrawLine (ray.origin, ray.origin + (ray.direction * hit.distance), Color.red, 2);
                                     //Debug.Log ("Chest Found");
                                     highlightContainer (true);

                             } else{
                                     highlightContainer (false);
                                     //Debug.Log ("Chest lost");
                                     }
 
 
 }


 private void highlightContainer(bool highlight){

     if (highlight) {

         if (components.Length > 0)
         
             for (int i = 0; i< _defaultColors.Length; i++) {
                 components[i].renderer.material.SetColor("_Color",Color.red);
                 components[i].renderer.material.shader = outlineShader;
                 components[i].renderer.material.SetFloat("_Outline", outlineSize);
                 components[i].renderer.material.SetColor("_OutlineColor", outlineColor);
             }


             } else {

         if (components.Length > 0)

             for (int i = 0; i< _defaultColors.Length; i++) {
                 components[i].renderer.material.SetColor("_Color",_defaultColors[i]);
                 components[i].renderer.material.shader = defaultShader;
             }


             }
     }


 

}

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

2 Replies

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

Answer by DaveA · Jan 09, 2014 at 11:48 PM

The problem is you are not looking for 'this specific chest' to be hit. Global events like key presses will be seen by all scripts and instances. And the camera seeing something that has a tag 'chest' will also be 'seen' by all instances. You need to check that the camera ray is hitting 'this' chest and change it's state only in that case.

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 itsboshytime · Jan 10, 2014 at 10:57 AM

Hmm i see what you mean but how do i check that it's hitting that specific chest? By using getcomponent? I've tried but it doesn't seem to work. Any additional info would be appreciated.

Edit: I've figured it out. I just replace the checking of the tag "chest" to something like this :

(hit.transform == gameObject.transform)

Thanks for the answer!

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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to programmatically copy selected text in InputField? 0 Answers

Change level script 2 Answers

Variables related to instance 0 Answers

Lots of animated Minecraft type characters on mobile 2 Answers

How to make a script full of methods provide methods independently for a number of gameobjects? 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