- Home /
Hide gameobject until certain condition has been met,Hide an object until a condition is met.
Hello, I am trying to hide an object "door" until all the enemies have been destroyed. From other questions I saw on here I opted for using renderer, but I keep getting an error message.
thank you ahead of time
MissingComponentException: There is no 'Renderer' attached to the "Door_Prefab" game object, but a script is trying to access it. You probably need to add a Renderer to the game object "Door_Prefab". Or your script needs to check if the component is attached before using it. Door.Update () (at Assets/Scripts/Door.cs:31)
void Update()
{
if(GameObject.FindWithTag("Enemy"))
{
gameObject.GetComponent<Renderer>().enabled = false;
}
else
{
gameObject.GetComponent<Renderer>().enabled = true;
}
}
What do you mean by gameObject.GetComponent().enabled
? That's incorrect any way.
It was simply a problem with the formatting, thus making <Renderer>
invisible
Answer by Hellium · Feb 24, 2018 at 11:11 PM
I think you should create a GameManager, always enabled, responsible for detecting when your enemies are destroyed. Then, your Door script will be warned by the GameManager once all the enemies are destroyed.
// Enemy.cs
using UnityEngine;
public class Enemy : MonoBehaviour, IPointerClickHandler
{
public event System.Action<Enemy> OnKilled;
void OnDisable()
{
if ( OnKilled != null )
OnKilled.Invoke( this );
}
}
// GameManager.cs
using UnityEngine;
public class GameManager : MonoBehaviour
{
private int remainingEnemies = 0 ;
public event System.Action OnLastEnemyKilled;
private void Start()
{
Enemy[] enemies = FindObjectsOfType<Enemy>();
for ( int i = 0 ; i < enemies.Length ; i++ )
{
AddEnemy( enemies[i] ) ;
}
}
private void AddEnemy( Enemy enemy )
{
remainingEnemies++ ;
enemy.OnKilled += RemoveEnemy ;
}
private void RemoveEnemy( Enemy enemy )
{
enemy.OnKilled -= RemoveEnemy ;
remainingEnemies-- ;
if( remainingEnemies == 0 && OnLastEnemyKilled != null )
OnLastEnemyKilled.Invoke() ;
}
}
// Door.cs
using UnityEngine;
public class Door: MonoBehaviour
{
// Drag & drop the object in the inspector
public GameManager GameManager ;
private void Awake()
{
GameManager.OnLastEnemyKilled += Show ;
Hide();
}
private void Show()
{
GameManager.OnLastEnemyKilled -= Show ;
gameObject.SetActive( true ) ;
}
private void Hide()
{
gameObject.SetActive( false ) ;
}
}
ORIGINAL ANSWER
Simply disable the gameobject instead of disabling the Renderer component:
gameObject.SetActive( GameObject.FindWithTag("Enemy") == null ) ;
However, keep in mind that calling the FindXXX
functions are not very efficient. If you have only few calls in your scene, it's fine.
Even if this is the only script where the Find function is called, it's terrible for performance as it's called every frame by Update.
The problem is that I can get the "door" start inactive at the beginning but once the all the enemies are killed off the door won't activate. I tried an if statement but still no luck
Answer by nathanvj · Feb 24, 2018 at 11:13 PM
If you don't want to disable the gameObject as @Hellium suggests, you need to enable & disable the MeshRenderer component.
gameObject.GetComponent<MeshRenderer>().enabled = true;
By the way: the GetComponent function is called every frame in the Update function. It's terrible for performance. You should store the MeshRenderer in a variable.
As indicated by the error, there is no Renderer attached to the gameobject so gameObject.GetComponent<$$anonymous$$eshRenderer>()
will return null
There's a difference between the $$anonymous$$eshRenderer and the Renderer.
Yes and no, $$anonymous$$eshRenderer
inherits from Renderer
, so $$anonymous$$eshRenderer
is a Renderer
$$anonymous$$issingComponentException: There is no '$$anonymous$$eshRenderer' attached to the "Door_Prefab" game object, but a script is trying to access it. You probably need to add a $$anonymous$$eshRenderer to the game object "Door_Prefab". Or your script needs to check if the component is attached before using it. Door.Update () (at Assets/Scripts/Door.cs:22)
I get this error message with the $$anonymous$$eshRenderer. Do I have to make a $$anonymous$$eshRenderer or what is it saying?
No, it's basically saying there's no $$anonymous$$eshRenderer component. But that's good to know. I'm just giving suggestions here. Could you try the following?
gameObject.renderer.enabled = false;
No worries, any assistance helps. And if gives the same error code, just says "renderer" this time ins$$anonymous$$d of meshrenderer
Your answer
Follow this Question
Related Questions
How can I control GameObject visibility? 2 Answers
Material class use general question 1 Answer
Rendermode: Cutout not working on gameobject from external asset bundle 2 Answers
Can't render GameObject 3 Answers
Disabling collider according to color? 2 Answers