How to make a game object invisible before trigger

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpriteReaction : MonoBehaviour
{
    private SpriteRenderer rend;

    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<Renderer>();
        rend.enabled = false ;
    }
 
    // Update is called once per frame
    void Update()
    {}
}

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameObjShow : MonoBehaviour 
{
    public GameObject customImage;
    private SpriteRenderer rend;

    // Start is called before the first frame update
    void Start()
    {
        customImage.SetActive(false);
        rend = customImage.GetComponent<SpriteRenderer>();
        rend.enabled = false;
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player");
        {
            customImage.SetActive(true);
            Debug.Log("On trigger enter 2D");
            rend.enabled = true;
        }
    }
    // Update is called once per frame
    void Update()
    {
    }
}

Sorry for the messy code, I am new to Unity and have been watching loads of videos to find the scripts…

Thanks!

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:

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.

Like I said, start putting Debug.Log() calls in… how about in Start()? is that runnign? etc.

the invisible trigger is working, only that the object (to be destroyed) isn’t invisible before it is called by the trigger :confused:

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.

“Play on Awake”

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.