- Home /
The question is answered, right answer was accepted
Disable child of clicked object
The following script identifies the object I click on and checks the tag on it. Now I am needing to know what I need to add to this to make it find the child game object and turn it off.
Here is the script:
using UnityEngine;
using System.Collections;
public class ChangeWeather : MonoBehaviour
{
public bool active = true;
private Transform child;
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == "WorldBlocks")
{
if(active == true)
{
foreach(Transform child in hit.transform)
{
child.Active = false;//This part is not working.
}
active = false;
}
if(active == false)
{
foreach(Transform child in hit.transform)
{
// child.Active = false; This part is not working
}
active = true;
}
}
}
}
}
}
^ In the latest versions of Unity you want to use .SetActive(false) ins$$anonymous$$d of .Active = false. Like Robertbu said, I tend to use child.renderer.enabled = false to hide it, but not disable it
Answer by davidflynn2 · Jul 15, 2013 at 08:50 PM
Ok this ended up working for me:
if(hit.collider.tag == "WorldBlocks")
{
if(active == true)
{
foreach(Transform child in hit.transform)
{
child.renderer.enabled = true;
}
active = false;
}
}
Answer by uskak41kw · Jul 15, 2013 at 07:42 PM
Also u can preSet this child as public GameObject
public GameObject child; //set it here
child.SetActive(false);
The child name will be the same but it could very what child is chose I have a world made up of different grids and I am wanting to be able to click on any of them. All I need it to do is take the object I click on find its child game object and turn it off.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Check if the child of an object is active not working!!! 2 Answers
C# Find component InChildren 3 Answers
How do i make my person move forward in the direction of the cursor 2 Answers