- Home /
Activating things- What on earth am I doing wrong???
Hey unity people! I am trying to activate and deactive a character on gui button clicks.
I am using Npc.active = true/false; , but when I deactivate it it only freezes the person. When I try to reactive it it tells me that there's a null reference exception...
I wan't to be able to completely turn off/on (not destroy) the npc and its children just like you can in the inspector window when you check the button for the object.
I have tried using npc.SetActiveRecursively(true) and its not working either. What am I doing wrong?
I am calling 'npc' with this
function Page4() {
Buttons();
npc = GameObject.FindWithTag("NPCmain");
GUI.Box (Rect (200,480,500,105), Name +"'s Team");
if (GUI.Button(Rect(210, 496, 150,30), " recall person1 ")){ npc.transform.position = transform.position + transform.right * -4; npc.transform.parent = transform; npc.active = false;
}if (GUI.Button(Rect(210, 510, 150,30), " release person1 ")){ //npc.active = true; npc.SetActiveRecursively(true);
npc.transform.parent = null;
}
This is part of a few on gui pages. I parent the npc to the main player so that when I move though different levels, they come with me. Any help is appreciated!
EDIT- I almost forgot, I need to beable to activate npcs that are already set to active = false at the start of the game if it is possible. Please help me out
$$anonymous$$ark the answer as accepted; there's no need to edit the title.
I tried to- it won't let me for some amount of hours yet
Answer by superventure · Mar 22, 2011 at 12:53 AM
Sometimes when I ask questions, I kind of hope people would try to think out of the box with me a little bit, but I guess beggars can't be choosers. I HAD to get this to work and stumbled across the answer:
I used SetActiveRecursively(false) to disable the npc, setting this line of code right after the positioning and parenting code lines to attach it in the player.
My problem was getting the npc to COME BACK. I was trying to call it back by directly referring to the disabled npc. SOOO, taking a second look at how 'SetActiveRecursively' works, turns out I was directing what to re activate to the wrong item. The solution- tell setactiverec. to fully activate the PLAYER. It reactivates ALL THE GAMEOBJECTS CHILDREN, including the npc I deactived and attached to it. Since I have multiple level items deactivated in the player as well I dont want setactiverec to re activate them also, so I'm going to have to create an empty game that will follow the player around to store npcs and such.
Hope this helps someone else in the future
Answer by DaveA · Mar 22, 2011 at 12:07 AM
When things go inactive (active == false), 'Find' will not find them, so that's why you get the null exception the next time. I think you want 'enable = false' instead, probably on any/all components in the object you need 'off'
That give me an error message (enabled is not a member of unity game object). There has to be some way this can work with acitve true/false?
With game object, you'd have to store the game object variable and set the active on that. With enabled, you'd GetComponent(s)(InChildren) and set/clear 'enabled' on them.
by the way, I just found out that 'Find' will find them with this- Npc = transform.Find("Npc").gameObject; Npc.SetActiveRecursively(true);
Your answer
Follow this Question
Related Questions
Platform moves when character steps on it 1 Answer
Is there any command that can reference deactivated objects? 1 Answer
Coroutine couldn't be started becuase inactive 0 Answers
how to activate/de-activate different scripts on a gameobject? 2 Answers
How to activate the child of an object without declaring a public transform? 1 Answer