- Home /
In a group of gameobjects, how to have only one object active at a time while disabling the others?
I'm working on a stealth game where there are multiple enemies in the scene - each with a vision cone.
So, what I want is to right click on an enemy to reveal his vision cone (a child), and disable any other vision cone gameobjects activated at that time. In other words, only one vision cone object can be active in the scene at a time
Cannot seem to find any reference for this - any suggestions, pointers would be greatly appreciated? Thanks in advance.
Answer by arrnav · May 09, 2020 at 10:10 AM
It is simple using an array of GameObjects.
Make a new Gameobject at the root of Hierarchy and attach a new script to it called "VisionCones.cs". Inside VisionCones.cs -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VisionCones : MonoBehaviour
{
public GameObject[] EnemyVisionCones;
public static VisionCones instance;
private void Awake()
{
instance = this;
}
public void ActivateConeDisableOthers(GameObject ParentEnemyObject)
{
foreach (GameObject VisionCone in EnemyVisionCones)
{
VisionCone.SetActive(false);
}
ParentEnemyObject.transform.GetChild(0).gameObject.SetActive(true); // Assuming the Vision Cone GameObject is the first child of it's parent Enemy GameObject, hence the "0" in GetChild(0)
}
}
Now create and attach a new script to each of your Enemy Parent GameObjects called "VisionConeClick.cs" -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VisionConeClick : MonoBehaviour
{
void OnClick()
{
VisionCones.instance.ActivateConeDisableOthers(this.gameObject);
}
}
Finally add a new event click on all of your Enemy GameObjects by adding OnClick() method from the their attached VisionConeClick.cs.
Let me know if this works or you have any more questions.
Thanks so much for the reply, and nicely explained solution! As I'm not very familiar with OnClick(), I used a ray cast in "VisionConeClick.cs" (as pasted below) to register mouse click. But it does not seem to be working
Is it essential to use OnClick() for this to work? In that case need to familiarize myself better with that
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VisionConeClick : $$anonymous$$onoBehaviour
{
public Layer$$anonymous$$ask cone$$anonymous$$ask;
private void Update()
{
RightClick();
}
void RightClick()
{
if (Input.Get$$anonymous$$ouseButtonUp(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100f, cone$$anonymous$$ask))
{
Viewcone$$anonymous$$anager.instance.ActivateConeDisableOthers(this.gameObject);
Debug.Log("Clicked"); // to check whether we register a click
}
}
}
}
Answer by rizuoo · May 09, 2020 at 11:49 AM
Got it to work! There was an error with my code
Replaced this
if (Physics.Raycast(ray, out hit, 100f, coneMask))
{
ViewconeManager.instance.ActivateConeDisableOthers(this.gameObject);
Debug.Log("Clicked"); // to check whether we register a click
}
With this
if (Physics.Raycast(ray, out hit, 100f, coneMask))
{
if (hit.collider.gameObject == this.gameObject)
ViewconeManager.instance.ActivateConeDisableOthers(this.gameObject);
}
Worked like a charm! Thanks so much, man! Much, mc
Sounds great.
Although I would suggest researching about & using Event Triggers on game objects further on for such simple interactions as Raycasting is a computationally expensive process in general and should only be used when really needed (Example for Ai$$anonymous$$g systems, Path-finding, etc)
Good luck!
Thanks for the advice - and here I've been raycasting left and right. Will surely do some more research on event triggers. Cheers!
Your answer
Follow this Question
Related Questions
Instantiate an array of gameobjects with a time delay between each 1 Answer
How would I select GameObjects from a List in a Dropdown Menu? 1 Answer
One List of GameObjects of different types... 0 Answers
Enemy AI to Discover Waypoints 1 Answer
adding components to a gameObject in a list of lists 2 Answers