- Home /
How to make object come back
Ok so I have a trigger that gets its renderer shut off when it is collected, but I don't know how to make it come back on when a different collectible is collected. How would I fix this?
void OnTriggerEnter(Collider other)
{
//green
if (other.gameObject.tag == "GreenCube")
{
other.gameObject.GetComponent<Renderer>().enabled = false;
isGreen = true;
ChangeColor();
}
//red
if (other.gameObject.tag == "RedCube")
{
other.gameObject.GetComponent<Renderer>().enabled = false;
isRed = true;
ChangeColor();
}
}
Answer by unity_ek98vnTRplGj8Q · Dec 16, 2019 at 05:20 PM
You are turning the renderer off but you aren't deleting the gameobject. You should be able to just call gameObject.GetComponent<Renderer>.enabled = true
. Note that disabling the renderer will not disable the trigger with the object, just will make it invisible to the camera.
From your snippet it looks like this script is interacting with some collectible objects, and that you want to re-enable them when some outside event happens, like a special collectible is found. If this is the case, an easy way to accomplish this is to add a script to each collectible:
public class CollectibleScript : Monobehavior{
private static List<Renderer> renderers;
void Start(){
if(renderers == null) renderers = new List<Renderer>();
renderes.add(GetComponent<Renderer>());
}
public static EnableAllRenderers(){
foreach(Renderer r in renderers){
r.enabled = true;
}
}
}
And then when you want to enable the renderers you can simply call CollectibleScript.EnableAllRenderers();
Thanks, I got my script to work before you submitted this so you answered just on time. I can apply this to make cubes to render back when they don't need to be rendered out.
Answer by WheresMommy · Dec 14, 2019 at 06:37 AM
Youncan make a public var on both cubes and drag drop the other ones in it to be able to access them.
ok I'll try that I guess. How do I get it to work with prefabs though because I have more script to this that turns doors into triggers but they won't work unless I put the door directly from the level ins$$anonymous$$d of the prefab.
Yeah, it doesn't want to work, it changed the prefab but not object linked to the prefab.
Edit: I tested to see if the cube in the level is linked to my prefab by changing its material and it switched.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
Flip over an object (smooth transition) 3 Answers
DirectX11 get handle to Unity Swapchain/Backbuffer 0 Answers