Can not show/hide a game object in Unity 3D V2018.3.9 f1
I have been trawling the the answer to this issue but can not find any way to get my script to hide/show a gameobject. I have tried many of the suggestions in other answers but to no avail. What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Artwork : MonoBehaviour
{
public SpriteRenderer artwork;
public int countsincestartup = 20;
public int numberofChildren;
private Rigidbody2D rb;
private int movingChildren;
private GameObject child;
private string childName;
private int startCounter = 0;
// Start is called before the first frame update
void Start()
{
artwork = GetComponent<SpriteRenderer>();
artwork.sortingOrder = 1;
}
private void Update()
{
//hide artwork after specified number of cycles
if (startCounter > countsincestartup)
{
gameObject.GetComponent<SpriteRenderer>().enabled = false;
}
else {
startCounter += 1;
}
//show artwork when all objects have stopped moving
movingChildren = numberofChildren;
for (int i = 1; i <= numberofChildren; i++)
{
if (i < 10)
{
childName = artwork.name.Replace("_00", "_0" + i.ToString());
}
else
{
childName = artwork.name.Replace("_00", "_" + i.ToString());
}
child = GameObject.Find(childName);
rb = child.GetComponent<Rigidbody2D>();
var childVelocity = rb.velocity;
if (childVelocity.sqrMagnitude < 0.01f)
{
movingChildren -= 1;
}
}
if (movingChildren == 0)
{
//all movement stopped
gameObject.GetComponent<SpriteRenderer>().enabled = true;
}
}
}
I wonder if this issue persists because he object I am trying to show or hide is a child object? Further investigations through the forums seems to indicate that I may have to call child objects differently from parent objects. Another project I tried earlier successfully used the game object.enable code to show and hide but this was on a parent object with no children.
Will move the child to a parent position and see.
Answer by dan_wipf · Mar 26, 2019 at 04:13 AM
i believe debugin your code, will get you to the desired result +> is the code stuck some where in an if statement and so on.. try use Debug.Log(=> something) to see if the of statements are acually working.
Dan_wipf, I have used Debug.Log in various places to ensure that the counter in incremented and that the if statement passes for hiding the game object. In both cases the answer is yes, the counter does increment and processing enters that part of the if statement to hide the gameobject.
As per my comment to made just prior to answering your suggestion, I will investigate if this is something to do with the game object being a child object of another gameobject.
Thanks for your suggestion though, it is appreciated.
well you might have messed up disabling the GameObject, with disabling the SpriteRenderer Component.
if you only disable the SpriteRenderer attached to the GameObject, the children Objects are still shown.
to really disable a GameObject you Need to call this: gameObject.SetActive(true/false);
this will result in as i believe you know that the gameObject and all it's children will be disabled/enabled in the hirarchy and all the Script's will be unaccessable due to disabling gameObject.
Answer by Vollmondum · Mar 26, 2019 at 05:26 AM
Best way to hide sprites is to assign empty material(image) and never mess up with enabling the renderer.
Vollmondum, thanks for the suggestion. I'll give it a try and see how this works.