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 /
avatar image
0
Question by Jacobsc · Jan 08, 2020 at 09:33 AM · objectobjectstagmultipletags

Can't get more than one object.

This script works fine until there is more than one door/cube in the scene. Only one of the objects will disappear (if two cubes, only one cube; if two doors, only one door). And yes, I have tried "FindGameObject*s*WithTag.

 public class Collection : MonoBehaviour
 {
     public Transform tForm;
     public GameObject gObject;
 
     private bool isYellow = false;
     private bool isRed = false;
     private bool isBlue = false;
     private bool isPurple = false;
 
     void OnTriggerEnter(Collider other)
     {
         //yellow
         if (other.gameObject.tag == "YellowCube")
         {
             isYellow = true;
             ChangeColorYellow();
         }
         //red
         if (other.gameObject.tag == "RedCube")
         {
             isRed = true;
             ChangeColorRed();
         }
         //blue
         if (other.gameObject.tag == "BlueCube")
         {
             isBlue = true;
             ChangeColorBlue();
         }
         if (other.gameObject.tag == "PurpleKey")
         {
             isPurple = true;
             ChangeColorPurple();
         }
     }
 
     private Color childColorYellow;
     private Color childColorRed;
     private Color childColorBlue;
     private Color childColorPurple;
 
 
     public void ChangeColorYellow()
     {
         childColorYellow = new Color(1, 0.92f, 0.016f, 1);
         transform.Find("Body").gameObject.GetComponent<Renderer>().material.color = childColorYellow;
         transform.Find("Front").gameObject.GetComponent<Renderer>().material.color = childColorYellow;
         isRed = false;
         isBlue = false;
         isPurple = false;
     }
 
     public void ChangeColorRed()
     {
         childColorRed = new Color(1, 0, 0, 1);
         transform.Find("Body").gameObject.GetComponent<Renderer>().material.color = childColorRed;
         transform.Find("Front").gameObject.GetComponent<Renderer>().material.color = childColorRed;
         isYellow = false;
         isBlue = false;
         isPurple = false;
     }
 
     public void ChangeColorBlue()
     {
         childColorBlue = new Color(0, 0, 1, 1);
         transform.Find("Body").gameObject.GetComponent<Renderer>().material.color = childColorBlue;
         transform.Find("Front").gameObject.GetComponent<Renderer>().material.color = childColorBlue;
         isYellow = false;
         isRed = false;
         isPurple = false;
     }
 
     public void ChangeColorPurple()
     {
         childColorPurple = new Color(1, 0, 1, 1);
         transform.Find("Body").gameObject.GetComponent<Renderer>().material.color = childColorPurple;
         transform.Find("Front").gameObject.GetComponent<Renderer>().material.color = childColorPurple;
         isYellow = false;
         isRed = false;
         isBlue = false;
     }
 
     public void Door_Cube()
     {
         if (isYellow == true)
         {
             //yellow
             GameObject.FindGameObjectWithTag("YellowCube").GetComponent<Renderer>().enabled = false;
             GameObject.FindGameObjectWithTag("YellowDoor").GetComponent<Collider>().isTrigger = true;
             GameObject.FindGameObjectWithTag("YellowDoor").GetComponent<Renderer>().material.color = new Color(1, 0.92f, 0.016f, 0.25f);
             //red
             GameObject.FindGameObjectWithTag("RedCube").GetComponent<Renderer>().enabled = true;
             GameObject.FindGameObjectWithTag("RedDoor").GetComponent<Collider>().isTrigger = false;
             GameObject.FindGameObjectWithTag("RedDoor").GetComponent<Renderer>().material.color = new Color(1, 0, 0, 1);
             //blue
             GameObject.FindGameObjectWithTag("BlueCube").GetComponent<Renderer>().enabled = true;
             GameObject.FindGameObjectWithTag("BlueDoor").GetComponent<Collider>().isTrigger = false;
             GameObject.FindGameObjectWithTag("BlueDoor").GetComponent<Renderer>().material.color = new Color(0, 0, 1, 1);
         }
         if (isRed == true)
         {
             //red
             GameObject.FindGameObjectWithTag("RedCube").GetComponent<Renderer>().enabled = false;
             GameObject.FindGameObjectWithTag("RedDoor").GetComponent<Collider>().isTrigger = true;
             GameObject.FindGameObjectWithTag("RedDoor").GetComponent<Renderer>().material.color = new Color(1, 0, 0, 0.25f);
             //yellow
             GameObject.FindGameObjectWithTag("YellowCube").GetComponent<Renderer>().enabled = true;
             GameObject.FindGameObjectWithTag("YellowDoor").GetComponent<Collider>().isTrigger = false;
             GameObject.FindGameObjectWithTag("YellowDoor").GetComponent<Renderer>().material.color = new Color(1, 0.92f, 0.016f, 1);
             //blue
             GameObject.FindGameObjectWithTag("BlueCube").GetComponent<Renderer>().enabled = true;
             GameObject.FindGameObjectWithTag("BlueDoor").GetComponent<Collider>().isTrigger = false;
             GameObject.FindGameObjectWithTag("BlueDoor").GetComponent<Renderer>().material.color = new Color(0, 0, 1, 1);
         }
         if (isBlue == true)
         {
             //blue
             GameObject.FindGameObjectWithTag("BlueCube").GetComponent<Renderer>().enabled = false;
             GameObject.FindGameObjectWithTag("BlueDoor").GetComponent<Collider>().isTrigger = true;
             GameObject.FindGameObjectWithTag("BlueDoor").GetComponent<Renderer>().material.color = new Color(0, 0, 1, 0.25f);
             //yellow
             GameObject.FindGameObjectWithTag("YellowCube").GetComponent<Renderer>().enabled = true;
             GameObject.FindGameObjectWithTag("YellowDoor").GetComponent<Collider>().isTrigger = false;
             GameObject.FindGameObjectWithTag("YellowDoor").GetComponent<Renderer>().material.color = new Color(1, 0.92f, 0.016f, 1);
             //red
             GameObject.FindGameObjectWithTag("RedCube").GetComponent<Renderer>().enabled = true;
             GameObject.FindGameObjectWithTag("RedDoor").GetComponent<Collider>().isTrigger = false;
             GameObject.FindGameObjectWithTag("RedDoor").GetComponent<Renderer>().material.color = new Color(1, 0, 0, 1);
         }
         if (isPurple == true)
         {
             //purple
             GameObject.FindGameObjectWithTag("PurpleKey").GetComponent<Renderer>().enabled = false;
             GameObject.FindGameObjectWithTag("PurpleDoor").GetComponent<Collider>().isTrigger = true;
             GameObject.FindGameObjectWithTag("PurpleDoor").GetComponent<Renderer>().enabled = false;
         }
     }
 
     void Update()
     {
         if (isYellow == true)
         {
             Door_Cube();
         }
         if (isRed == true)
         {
             Door_Cube();
         }
         if (isBlue == true)
         {
             Door_Cube();
         }
         if (isPurple == true)
         {
             Door_Cube();
         }
     }

Comment
Add comment · Show 2
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 Jacobsc · Jan 07, 2020 at 03:20 AM 0
Share

btw sorry for it being so long, idk where the error may be.

avatar image Ady_M · Jan 08, 2020 at 12:20 PM 1
Share

You wrote that you've already tried using FindGameObjectsWithTag (which returns an array). Why did you abandon that idea?? :)

Try again and post the code if it doesn't work. Remember to use a loop.

As a test, make it so that you can press a key to show/hide objects of a specific color.

When all is done and fixed, look into enums, my friend. Those bools you're using (isYellow, isBlue, etc) will be giving me many sleepless nights ;)

1 Reply

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

Answer by logicandchaos · Jan 08, 2020 at 04:20 PM

A little more description of your problem would help, I'm not sure what you are trying to achieve and I'm not sure what object this code is on. Are you sure all your objects have the correct tags? I would make your color bools into a enum though, that will prevent you from have two or more true at the same time. You seem to be trying to set up a finite state machine, you should set up the enum enum ColorState{ YELLOW,RED,BLUE,PURPLE}; ColorState isColor; then you switch statements instead of your if statements will clean things up. Also all you change color methods could be one method that takes a parameter:

public void ChangeColor(Color p_color, ColorState p_colorState){ transform.Find("Body").gameObject.GetComponent().material.color = p_color; transform.Find("Front").gameObject.GetComponent().material.color = p_color; isColor=p_colorState; }

Are you trying to turn on all the objects that share a color on or off? If so then GameObject.FindGameObjectWithTag is returning one object.. you would have to make a List of all the objects that have that tag and then turn them all off in a loop. But what you could do.. a more C# solution would be to make a static function that shows and hides your objects, so when it gets called on one yellow box it gets called on all yellow boxes, this would necessitate a different script for each colored box.. Again I'm not entirely sure what you are trying to accomplish I hope my input helps.

Comment
Add comment · Show 10 · 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 Jacobsc · Jan 10, 2020 at 02:54 AM 0
Share

The issue is is that when I try to open two blue doors, only one will open. I've been trying foreach loops but I can't get them to work. Everything worked until I placed to doors of the same color into the map, which made me upset because I thought I wouldn't have to touch that script again.

avatar image Ady_M Jacobsc · Jan 10, 2020 at 04:15 AM 0
Share

Show us what the code looks like when you use a loop.

avatar image Jacobsc Ady_M · Jan 10, 2020 at 04:58 PM 0
Share

still has some work. For some reason the public List won't become a public value that I can put the prefab into.

Show more comments

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

126 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

Related Questions

How do I affect every object with a certain tag? 0 Answers

how can i find the closest object (with the tag Point) 1 Answer

Deleted sub-object causing lag 1 Answer

Changing tagged objects in script 1 Answer

How to assign a tag to multiple parts of a Model 0 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