- Home /
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();
}
}
btw sorry for it being so long, idk where the error may be.
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 ;)
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.
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.
Show us what the code looks like when you use a loop.
still has some work. For some reason the public List won't become a public value that I can put the prefab into.