Hi I have the following problem. I want to make a GameObject invisible when the user hits the return key, but everything I tried doesn’t work. I tried looping through the children, but it just makes the first one invisible (I switched the order of the children to be sure, but everytime just the first one gets invisible) with this Code:
public class textscript : MonoBehaviour
{
CanvasGroup m_CanvasGroup;
SpriteRenderer m_SpriteRenderer;
// Start is called before the first frame update
void Start()
{
m_CanvasGroup = GetComponent<CanvasGroup>();
m_SpriteRenderer = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.Return)){
Hide();
}
}
void Hide(){
m_CanvasGroup.alpha = 0f;
m_CanvasGroup.blocksRaycasts = false;
var tempColor = m_SpriteRenderer.color;
tempColor.a = 0f;
m_SpriteRenderer.color = tempColor;
GameObject Parent = this.gameObject;
int x;
for( x = Parent.transform.childCount; x > 0; x--);
{
Parent.transform.GetChild(x).GetComponent<SpriteRenderer>().color = tempColor;
}
}
void Show(){
m_CanvasGroup.alpha = 1f;
m_CanvasGroup.blocksRaycasts = true;
}
}
I tried to use a MeshRenderer with this Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class textscript : MonoBehaviour
{
MeshRenderer m_MeshRenderer;
SpriteRenderer m_SpriteRenderer;
// Start is called before the first frame update
void Start()
{
m_MeshRenderer = GetComponent<MeshRenderer>();
m_SpriteRenderer = GetComponent<SpriteRenderer>();
Show();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.Return)){
Hide();
}
}
void Hide(){
m_MeshRenderer.enabled = false;
}
void Show(){
m_MeshRenderer.enabled = true;
}
}
This didn’t work either(Now nothing got invisible) . Here’s my GameObject in the Prefab mode:
And I have run out of ideas to try. Can anyone help me?
Thanks in advance for any help
