- Home /
I need a script to disable/hide one child game object inside a parent when you exit a trigger collider.
Alrighty, so for some context:
I have a First Person Controller. It's children are Graphics and Main Camera. The First Person Controller has an inactive script called Rain Creator on it. The game starts. I move into 'Area1' and that causes Rain Creator to turn on. Rain Creator makes a clone of a prefab in my Assets (Heavy Rain) and then makes it a child of First Person Controller. I leave 'Area1' and the Rain Creator script turns off, but Heavy Rain (Clone) is still a child of First Person Controller.
So I want to add to the Rain Creator script that when I leave 'Area1' the child object 'Heavy Rain(Clone)' is hidden so that when I walk back into 'Area1' it will unhide that same child object. I do NOT want to delete/destroy the child object, because if I do the Rain Creator script will not make another when it gets re-activated.
Is there a way for a 'ForEach' loop to only target one child object and then disable it? I think that's what I need to do but I can't figure out how. C# preferred but I could probably get javascript to work as well.
P.S. These suggested tags are terrible for this post >_>
-1, Unity Answers is not here to write your scripts for you. I will also do my best to downvote any answer that contains more than 5 lines of code.
Well it seems to be here to help write scripts for everyone else, but you must fight for what you feel is right of course. I have not the time to learn coding from the ground up, I merely enjoy creating visions that I have seen in my $$anonymous$$d. If there are those that would help me accomplish my vision then I will always say more power to 'em.
$$anonymous$$y policy is downvoting every question asking for a working, complete, script. I consider it very rude.
If someone asks for a little help in implementing a small feature and someone, out of the goodness of their heart, gives the OP working code, it's fine.
I think this question crosses that line between asking for a script and a little help, by outright asking for a script. Thus I'm taking a stand.
Answer by aldonaletto · Oct 06, 2013 at 04:25 PM
You should change a little your logic: instead of enabling/disabling RainCreator, activate/deactivate heavyRain (create it if necessary when entering Area1) - like this:
using UnityEngine;
using System.Collections;
public class RainCreator: MonoBehaviour {
public GameObject heavyRainPrefab; // heavy rain prefab
private GameObject heavyRain;
void OnTriggerEnter(Collider other){
if (other.name == "Area1"){ // entering Area1:
if (heavyRain){ // if heavyRain already exists...
heavyRain.SetActive(true); // activate it
} else { // if doesn't exist yet, create it:
heavyRain = Instantiate(heavyRainPrefab, transform.position, transform.rotation) as GameObject;
}
}
}
void OnTriggerExit(Collider other){
if (other.name == "Area1"){ // leaving Area1:
heavyRain.SetActive(false); // deactivate heavy rain
}
}
}
I removed my answer as the logic in this one is much more efficient.
That script almost did it. I needed the created prefab to become a child of the First Person Controller and have it move to its location.
I added a 'public Vector3 localPosition;' and then under the instantiate I put heavyRain.transform.parent = transform; heavyRain.transform.localPosition = localPosition;
It now becomes a child and is moved to the Controllers location... but for some reason the rain prefab has become a super tiny slice, so it's like a 4x1 now ins$$anonymous$$d of a 4v4.
If I just normally place the prefab onto the controller it works, and if I use the old rain creator script to make a heavy rain prefab it works ($$anonymous$$eaning it's large), but for some reason even if I try and enlarge the one that is created by your script, it just gets taller, not wider.
Edit: Here are some pictures to try and explain a bit better, although it's nearly impossible to take them in my scene.
Working: http://i.imgur.com/D15Cymb.jpg
If you zoom in you can see tiny particles floating down in a large radius around the first person controller.
Not Working: http://i.imgur.com/UsiRRll.jpg
Now, only a very small straight line is cutting through the first person controller.
It dawns on me now that maybe I should've said the Heavy Rain prefab is a particle system? I don't know if that changes anything.
Edit: Edits are plentiful! The transform.rotation in the original script causes it to turn into a tiny slice, now I just need to figure out how to make it transform to an altered rotation or keep its original rotation and not adopt the rotation of the first person controller.
The Last edit!: I just removed the transform.position and transform.rotation from the instantiate line and now everything is right as rain! All puns intended! Thank you very much!
try changing
heavyRain = Instantiate(heavyRainPrefab, transform.position, transform.rotation) as GameObject;
to
heavyRain = Instantiate(heavyRainPrefab, transform.position, heavyRainPrefab.transform.rotation) as GameObject;
so you're calling the rains rotation ins$$anonymous$$d of the players. This might fix it.
Answer by rafasp · Oct 05, 2017 at 03:57 AM
You need Transform
Transform imageaux = gameobjaux.transform.Find("Image"); imageaux.gameObject.SetActive(false);
Your answer
Follow this Question
Related Questions
How to remove the oldest object 0 Answers
Rigidbody Not Move 0 Answers
Enable Instantiated Object 1 Answer
Scriptable object saves but what about objects it references? 0 Answers
Scriptable objects has empty inspectors and do not exist in build 4 Answers