- Home /
How to hide/unhide an array of objects with a mouse click.
I am trying to write a C# scrip that allows me to hide with left click then unhide with right click game objects. I have tried looking everywhere but nothing seems to be coming up. there is an array of game objects that have to be hidden/unhidden and the script is bound to the player. This is as far as I got:
using UnityEngine;
using System.Collections;
public class RealmSwitch : MonoBehaviour {
public Light lt;
public GameObject[] Realm1;
public GameObject[] Realm2;
// Use this for initialization
void Start () {
lt = GetComponent<Light>();
lt.color = Color.red;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0))
{
foreach (GameObject go in Realm1) {
GetComponent<MeshRenderer>().enabled = true;
}
foreach (GameObject go in Realm2) {
GetComponent<MeshRenderer>().enabled = false;
}
lt.color = Color.blue;
}
if (Input.GetMouseButtonDown (1))
{
foreach (GameObject go in Realm1) {
GetComponent<MeshRenderer>().enabled = false;
}
foreach (GameObject go in Realm2) {
GetComponent<MeshRenderer>().enabled = true;
}
lt.color = Color.red;
}
}
}
When you call GetComponent(), you get the $$anonymous$$eshRenderer component of the current object
Try go.GetComponent<$$anonymous$$eshRenderer>()
Your answer
Follow this Question
Related Questions
Add two radius on the same shader 1 Answer
An Instantiated object to array problem 2 Answers
How to display a list of methods and allow a choice of one of them 1 Answer
how can i check if there is a blanc spot in my list 3 Answers
How do I make an array from text objects and get them to instantiate randomly? 1 Answer