Home>

Currently, I am attaching an object to be scripted and displayed on an empty game object.

Parent: Empty game object
Child: Object to be displayed (Hidden start)
Tap 10 times to show the object

I don't get any error, but the object is not displayed even after the specified number of taps.
I would appreciate it if you could teach me how to fix it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character: MonoBehaviour
{
    [SerializeField] GameObject Character01;
    private int counter;
    void Update ()
    {
        if (Input.GetMouseButtonDown (0))
        {
            counter ++;
            if (counter<= 10)
            {
                // Hide game object → Show
                gameObject.SetActive (true);
            }
        }
    }
}
  • Answer # 1

    A gameObject (lowercase) is a variable inherited from MonoBehaviour and refers to the GameObject to which this script is attached.
    The reference to the GameObject set in the Inspector is in Character01.
    (And the inequality sign is the opposite.)

  • Answer # 2

    Is the child object correct with the recognition that it points to "Character01"?
    If it matches, it will be displayed below.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Character: MonoBehaviour
    {
        [SerializeField] GameObject Character01;
        private int counter;
        void Update ()
        {
            if (Input.GetMouseButtonDown (0))
            {
                counter ++;
                // if (counter<= 10) * The inequality sign is reversed (it is less than 10 times)
                if (counter>= 10)
                {
                    // Hide game object → Show
                    // gameObject.SetActive (true);* With the description method on the left, the "empty game object" to which the script is attached will be the target of Active.
                    Character01.SetActive (true);
                }
            }
        }
    }