Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Digital-Phantom · Feb 07, 2015 at 08:58 PM · c#setactivechild objectgetmousebuttondownselected

Why is Child Object not being set to Active (SetActive(true) on mouse click ? (Solved)

The following script is supposed to allow the player to select/deselect an object by clicking the left mouse button.

(to be precise it enables/Sets to active a child object that has a projector component attached)

Im not getting any compiler errors but the script just doesn't work. When I click on the units/characters in game I get the correct debug message ("Found Unit !") but the child object is not getting activated ?

 using UnityEngine;
 using System.Collections;
 
 public class Mouse : MonoBehaviour
 {
     RaycastHit hit;
 
     public static GameObject CurrentlySelectedUnit;
     
     public GameObject Target;
 
     private Vector3 mouseDownPoint;
 
 
     void Awake()
     {
         mouseDownPoint = Vector3.zero;
     }
     
     void Update()
     {
         
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(Physics.Raycast(ray, out hit, Mathf.Infinity))
         {
             // Store point(co-ordinates) at left mouse button down
             if(Input.GetMouseButtonDown(0))
             {
                 mouseDownPoint = hit.point;
             }
 
             if(hit.collider.name == "TerrainMain")
             {
                 // Instantiate Target when user clicks the Left mouse button
                 if(Input.GetMouseButtonDown(1))
                 {
                     GameObject TargetObj = Instantiate(Target, hit.point, Quaternion.identity) as GameObject;
                     TargetObj.name = "Target Instantiated";
                 }
 
                 else if(Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
                     DeselectGameobjectIfSelected();
 
             } // End of the Terrain
 
         else
 
         {
             // Hitting Other Objects
             if(Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
             {
                 // Is the User Hitting a Unit?
                 if(hit.collider.transform.FindChild("Selected"))
                 {
                     // Found a Unit we can Select !
                     Debug.Log("Found Unit !");
 
                     // Are we Selecting a Different Object?
                     if(CurrentlySelectedUnit != hit.collider.gameObject)
                     {
                         // Activate the Selector
                         GameObject SelectedObj = hit.collider.transform.FindChild("Selected").gameObject;
                         SelectedObj.SetActive(true);
 
                         // Deactivate the Currently Selected Objects Selector
                         if(CurrentlySelectedUnit != null)
                                 CurrentlySelectedUnit.transform.FindChild("Selected").gameObject.SetActive(false);
 
                         // Replace Currently Selected Unit
                             CurrentlySelectedUnit = hit.collider.gameObject;
                     }
 
                 } else 
 
                     {
                 
                         // If this Object is not a Unit
                         DeselectGameobjectIfSelected();
                     }
             }
 
         }
 
         }
 
         else
 
         {
             if(Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
                 DeselectGameobjectIfSelected();
         }
         
         Debug.DrawRay(ray.origin, ray.direction * Mathf.Infinity, Color.yellow);
     }
 
     #region helper functions
     
     public bool DidUserClickLeftMouse(Vector3 hitPoint)
     {
         float clickZone = 50.0f;
         
         if(
             (mouseDownPoint.x < hitPoint.x + clickZone && mouseDownPoint.x > hitPoint.x - clickZone) &&
             (mouseDownPoint.y < hitPoint.y + clickZone && mouseDownPoint.y > hitPoint.y - clickZone) &&
             (mouseDownPoint.z < hitPoint.z + clickZone && mouseDownPoint.z > hitPoint.z - clickZone)
           )
             return true; else return false;
     }
     
     //deselcts gameobject if selected
     public static void DeselectGameobjectIfSelected()
     {
         if(CurrentlySelectedUnit != null)
         {
             CurrentlySelectedUnit.transform.FindChild("Selected").gameObject.SetActive(false);
             CurrentlySelectedUnit = null;
         }
     }
     
     #endregion 
 
 
 
 }
 

Any ideas as to why the child object is not being SetActive(true) ?

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 Digital-Phantom · Feb 08, 2015 at 06:05 AM

Ok... I'm officially an idiot !

It is working, what I didn't realise is that by default projector components are set to disabled. Therefor I was actually selecting/deselecting the units... I just wasn't get any visual confirmation of this as the projector never displayed the graphic.

Once I set the projector component to active/enabled and applied that to my prefabs everything works fine !

Comment
Add comment · 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
1

Answer by Naphier · Feb 08, 2015 at 06:06 AM

If it's not active when you use FindChild I'm pretty sure it won't be found. So if you can't attach a public reference to it on this script then you'll want to iterate through the transforms in the hit.collider.transform like so:

 GameObject selectedChild;
 foreach (Transform t in hit.collider.transform)
 {
     if (t.name == "Selected")
     {
         selectedChild = t.gameObject;
         break;
     }
 }

 if (selectedChild != null)
 //do stuff

Furthermore a quick look at the scripting documentation has nothing on FindChild, so you probably shouldn't be using it or it was deprecated long ago. Good luck!

EDIT: Surprised it works, but I'm glad it does for you.

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 Digital-Phantom · Feb 08, 2015 at 06:12 AM 0
Share

@Naphier thanks for your help.

I was beginning to think it was something to do with it being written in an older version of Unity as the youtube tutorials I'm following are about 18 months old.

Its really frustrating when you follow a tutorial word for word and you then get errors, its even worse when you don't get errors though as you have no idea where to start looking.

avatar image Naphier · Feb 08, 2015 at 06:15 AM 0
Share

Agreed! I've only been at Unity for about a month and old tutorials can really stink.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Enabling/Deactivating objects using keys. 1 Answer

How to Use DontDestroyOnLoad on my C# Script? 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer


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