- Home /
C# disable 2 diffirent scripts OnTriggerEnter
Hello, Im trying to disable 2 diffirentscripts when entering my keypad(its a OnTriggerEnter) and enabling them when clicking on the GUI button Ok after typing in the code to open the door. The scripts I want to disable is the SmoothMouseLook script which I got on my FPScontroller and the other script I want to disable is on my camera which is a MouseLook script (my Camera is a child of my FPS controller). How would I write if I want to access both of them and disable them OnTriggerEnter? I tried putting GetComponent().enabled = false; but Im getting an error, Object reference not set to an instance of an object.
Anyone can help me out here?
http://answers.unity3d.com/questions/675988/c-pausing-the-game-during-gui-keypad.html
You can't do GetCoponent(), you have to be specific about the component name
Show the exact code you are using (in case the above is a cut/paste/formatting error).
http://answers.unity3d.com/questions/675988/c-pausing-the-game-during-gui-keypad.html is one of my questions but I still don't really understand how this getcomponet actually works.
Answer by getyour411 · Mar 30, 2014 at 08:17 PM
The two questions are essentially the same; when OnTriggerXX fires you have a something like col : Collider; with "col" you have access to the GameObject and can walk the hierarchy, such as
col.gameObject.GetComponent<SmoothMouseLook>().enabled = false;
col.gameObject.transform.GetChild("childNodeName").GetComponent<MouseLook>().enabled = false;
change childNodeName to be the name of the child node in your player hierarchy that the camera is attached to. Adjust any syntax nuances as needed, the above is off the top of my head.
http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/
Im getting this error while using col.gameObject.transform.GetChild("FirstPersonController").GetComponent().enabled = false; 2 errors on that > The best overloaded method match for UnityEngine.Transform.GetChild(int)' has some invalid arguments second error > error CS1503: Argument
#1' cannot convert string' expression to type
int'
Im a retard so please explain to me like if you would explain to a monkey or something. Im greatfull someone is trying to explain this to me though.
http://docs.unity3d.com/Documentation/ScriptReference/Transform.GetChild.html
You can only use GetChild to get a child by it's index. $$anonymous$$aybe you could try GetComponentsInChildren or GetComponentInChildren.
eg If you only have one $$anonymous$$ouseLook component (which you should) you can use:
col.gameObject.transform.GetComponentInChildren<$$anonymous$$ouseLook>().enabled = false;
col.gameObject.GetComponent<Smooth$$anonymous$$ouseLook>().enabled = false;
col.gameObject.transform.GetComponentInChildren<$$anonymous$$ouseLook>().enabled = false;
I got those now on a OnTriggerEnter, The Smoothmouselook gets disabled, but I can still move my camera up and down as my $$anonymous$$ouseLook seems not to get disabled, Im not getting any errors at all though..
I said the above was off the top of my head and adjust any syntax mods as needed; use transform.Find there are literally thousands of examples on UA/Google showing how to use GetComponent.
Your answer
Follow this Question
Related Questions
all my scripts stopped working after i put a enemy spawner script in 0 Answers
How to access Vive controllers in a script attached on a simple game object(a cube)? 0 Answers
Object reference not set to an instance of an object 1 Answer
Property guiTexture has been deprecated . Use GetComponent() 1 Answer
can not access variable of prefab script 0 Answers