- Home /
Prefabs won't change?
So I'm trying to make all of my prefabs to turn off their renderers and others turn into triggers, but the prefab changes but the in-game objects linked to the prefab does not. Here are some lines that are related:
public GameObject m_GreenDoor;
public GameObject m_RedDoor;
public GameObject m_GreenCube;
public GameObject m_RedCube;
void OnTriggerEnter(Collider other)
{
//green
if (other.gameObject.tag == "GreenCube")
{
m_GreenCube.GetComponent<Renderer>().enabled = false;
m_RedCube.GetComponent<Renderer>().enabled = true;
isGreen = true;
ChangeColor();
}
//red
if (other.gameObject.tag == "RedCube")
{
m_RedCube.GetComponent<Renderer>().enabled = false;
m_GreenCube.GetComponent<Renderer>().enabled = true;
isRed = true;
ChangeColor();
}
}
public void OpenDoor()
{
if (isGreen == true)
{
m_GreenDoor.GetComponent<Collider>().isTrigger = false;
}
if (isRed == true)
{
m_RedDoor.GetComponent<Collider>().isTrigger = false;
}
}
I have just realized another issue, if I collect green (turns ship green), then red (turns ship red) I can't turn back to green. If you have some input that would be helpful.
What does the ChangeColor() function do? If it's changing the other's tag then you might have an issue with the second if condition firing. I'd change that to an "else if" and see if that helps.
As far as your main issue goes, you are just changing the prefabs themselves which is why things in the scene aren't changing. At runtime all prefabs are uncoupled from their source prefab I think. You might want to grab a list or array of all green doors, green cubes, etc. at the beginning of the scene or something and iterate through that as needed.
Oh, that changes the color of the character, I should have removed those to make it clear. Those aren't related. Sorry about that. The goal is to hide the cube by turning the renderer off until a new cube is collected. Also is there an easier way to do this other than just listing every cube type? (there will be more later). This is my first game so I'm going with a simple color swap game.
Ok so the script it kinda working, I left for an hour and the red cube's renderer turned off with the game off. Then I tried to fix it but ended up doing nothing but did a test for fun. I then came back and an hour later and the green cube's renderer turned off. I am really confused on what is going on.
Answer by ThomasOr · Dec 15, 2019 at 11:00 AM
You shouldn't be making changes via the prefab system, that can fork up your game. Instead you should use FindWithTag or some other GameObject.find to get every instance of your prefab and then a foreach loop to iterate your changes over them.
I suspect that you actually don't have multiple of the same instance of a prefab however, so you should just drag and drop the in-scene objects to the gameObject slots and not the root prefabs. You should only use the root prefabs when you will be instantiating it at least one time which you are not.
Well currently I only have one gameObject, but when I add more levels I will need to change many Objects at the same time. But it works now, thanks!
Your answer
Follow this Question
Related Questions
Unity Programmatically Link Prefab 2 Answers
Link GUIText to a prefab? 2 Answers
Prefabs spawn with wrong values 0 Answers