• 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 Scooby8523 · Feb 24, 2018 at 10:18 PM · gameobjectrenderervisible

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;
     }
 }
Comment
Add comment · Show 3
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 nathanvj · Feb 24, 2018 at 11:05 PM 0
Share

What do you mean by gameObject.GetComponent().enabled? That's incorrect any way.

avatar image Hellium nathanvj · Feb 24, 2018 at 11:10 PM 0
Share

It was simply a problem with the formatting, thus making <Renderer> invisible

avatar image nathanvj Hellium · Feb 24, 2018 at 11:13 PM 0
Share

Aha. Thanks you.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · 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 nathanvj · Feb 24, 2018 at 11:20 PM 0
Share

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.

avatar image Scooby8523 · Feb 25, 2018 at 12:06 AM 0
Share

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

avatar image
-1

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.

Comment
Add comment · Show 7 · 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 Hellium · Feb 24, 2018 at 11:23 PM 0
Share

As indicated by the error, there is no Renderer attached to the gameobject so gameObject.GetComponent<$$anonymous$$eshRenderer>() will return null

avatar image nathanvj Hellium · Feb 24, 2018 at 11:25 PM -1
Share

There's a difference between the $$anonymous$$eshRenderer and the Renderer.

avatar image Hellium nathanvj · Feb 25, 2018 at 09:43 AM 0
Share

Yes and no, $$anonymous$$eshRenderer inherits from Renderer, so $$anonymous$$eshRenderer is a Renderer

avatar image Scooby8523 · Feb 25, 2018 at 12:10 AM 0
Share

$$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?

avatar image nathanvj Scooby8523 · Feb 25, 2018 at 12:13 AM -1
Share

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;
avatar image Scooby8523 nathanvj · Feb 25, 2018 at 01:20 AM 0
Share

No worries, any assistance helps. And if gives the same error code, just says "renderer" this time ins$$anonymous$$d of meshrenderer

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

112 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

Related Questions

How can I control GameObject visibility? 2 Answers

line render from object to object 1 Answer

performance between disable renderer and disable gameObject 2 Answers

Turning off renderer in other game object (C#) 2 Answers

How do I use RayCasting to play a sound when my player is close to an enemy 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges