Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make a game object invisible before trigger

Discussion in 'Scripting' started by kl852, May 6, 2021.

  1. kl852

    kl852

    Joined:
    Oct 12, 2020
    Posts:
    10
    Hi, I am working on a 2D mini game and have issue with sprite renderer.

    In the game, when the Player steps on an invisible trigger, a game object will be prompted (and later to be destroyed with Space bar).

    I have added the game object into the scene at where it's supposed to be, and applied the following on the object:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpriteReaction : MonoBehaviour
    6. {
    7.     private SpriteRenderer rend;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         rend = GetComponent<Renderer>();
    13.         rend.enabled = false ;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {}
    19. }
    However, in the inspector, the "sprite renderer" box is automatically checked as I pushed the play button. When I disabled it and let the player step on the invisible trigger, the object shows, which is the correct behavior.

    May I ask how to make the object invisible until the player steps on the trigger?

    Here's the script for the trigger:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameObjShow : MonoBehaviour
    6. {
    7.     public GameObject customImage;
    8.     private SpriteRenderer rend;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         customImage.SetActive(false);
    14.         rend = customImage.GetComponent<SpriteRenderer>();
    15.         rend.enabled = false;
    16.     }
    17.     void OnTriggerEnter2D(Collider2D other)
    18.     {
    19.         if (other.gameObject.tag == "Player");
    20.         {
    21.             customImage.SetActive(true);
    22.             Debug.Log("On trigger enter 2D");
    23.             rend.enabled = true;
    24.         }
    25.     }
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.     }
    30. }
    Sorry for the messy code, I am new to Unity and have been watching loads of videos to find the scripts...

    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,768
    This should work... UNLESS this script is on the same GameObject that you call
    customImage


    If so it might fail, but I thought the documentation promises OnTriggerEnter2D will get called on inactive objects. Not sure.

    Maybe your tags are wrong. Maybe it's on the wrong player. Either way, you need to get more insight into what's happening at runtime.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    You could also just display various important quantities in UI Text elements to watch them change as you playtest.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. kl852

    kl852

    Joined:
    Oct 12, 2020
    Posts:
    10

    Thanks for your quick reply!
    I believe the code on the invisible trigger works because the object has been triggered to show. There's also a public field where I can dropped the related object into it...
    Unfortunately it doesn't remain invisible at the beginning of the game.
    I have tried UI but it's very confusing on the scene therefore I tried to use the scripts above.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,768
    Like I said, start putting Debug.Log() calls in... how about in Start()? is that runnign? etc.
     
  5. kl852

    kl852

    Joined:
    Oct 12, 2020
    Posts:
    10

    the invisible trigger is working, only that the object (to be destroyed) isn't invisible before it is called by the trigger :/
     
  6. openblindmusic

    openblindmusic

    Joined:
    May 22, 2021
    Posts:
    3
    I am having the same issue. I want to have the object not appear at start. Kind of like unchecking the "on awake" box in audio Source.
     
  7. openblindmusic

    openblindmusic

    Joined:
    May 22, 2021
    Posts:
    3
    "Play on Awake"
     
  8. openblindmusic

    openblindmusic

    Joined:
    May 22, 2021
    Posts:
    3
    Figured it out! Uncheck the sprite renderer box, then add component :"interact on 2d button". In ON Enter() Drag object from hierarchy to the field under runtime only. Choose "spriterenderer.enabled" from drop down and check the box. Starts as invisible and turns on when triggered. use "spriterenderer.forcerenderingoff" in ON Exit() to turn it off again.