Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rizuoo · May 09, 2020 at 09:21 AM · gameobjectslistsarray of gameobjects

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rizuoo · May 09, 2020 at 11:37 AM 0
Share

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
             }
 
         }
     }
 }
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image arrnav · May 09, 2020 at 03:00 PM 0
Share

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!

avatar image rizuoo arrnav · May 09, 2020 at 05:45 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges