- Home /
The question is about script interaction and has many answers already.
Accessing scripts in the same game object
I am trying to disable other scripts in the same game object, some are C# others are java. I have tried the code in the documentation
Charcontdisable = GetComponent.<MouseLook>();
but this does not seem to work, what would be ideal for what I am trying to achieve?
Answer by Cherno · May 10, 2015 at 11:09 PM
A syntax error, and make sure that Charcontdisable is of Type MouseLook.
MouseLook Charcontdisable = GetComponent<MouseLook>().enabled = false;
So will this work with both java and C#, and to enable I just switch the boolean from false to true?
This is for C#.
Check the doc for UnityScript version of GetComponent
I get these errors using that code
Assets/scr_P_Health.cs(39,27): error CS0029: Cannot implicitly convert type `bool' to `$$anonymous$$ouseLook'
Assets/scr_P_Health.cs(17,45): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
Don't get me wrong, but you need to learn the basic of program$$anonymous$$g before using Unity. Even for a basic project. Unity is not $$anonymous$$odu, it requires program$$anonymous$$g knowledge. I would suggest to spend some time practicing C# and program$$anonymous$$g tutorial.
Answer by zckhyt · May 11, 2015 at 09:18 AM
if you're trying to disable "MouseLook," simply
GetComponent<MouseLook>().enabled = false;
As far as I know, you can't assign the component of an object to a value. Also, there is a typo in your code where you include a 'dot' after GetComponent; the proper syntax is GetComponent<"Component">();
enabled is a public member of all components that toggles whether or not the script is used (roughly speaking). So from any component on the same object you just need to use GetComponent<"Component">().enabled = true/false; to either enable it or disable it.
edit: The angulary brackets were removed by the system because it uses them for text-formatting. This edit puts all the uses into a special field to prevent that.
I get these errors when I attempt to use this
Assets/scr_P_Health.cs(18,17): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
Assets/scr_P_Health.cs(40,25): error CS1502: The best overloaded method match for `UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments
Assets/scr_P_Health.cs(40,25): error CS1503: Argument `#1' cannot convert `bool' expression to type `System.Type'
I don't think you used it correctly. $$anonymous$$ake sure you have GetComponent called from the same object as the object with the component that you are trying to enable/disable. Then, within the <>, supply the component's name (in this case it should be $$anonymous$$ouseLook). This is followed by the two parentheses (which should be empty). Now you can access any public member function or variable of the component.
e.g.,
GetComponent<$$anonymous$$ouseLook>().enabled = false;
source: http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
edit: for some reason it's removing the <> from the code I'm trying to provide, which explains what happened to them in my original comment. I'll try putting them into a special field.
Answer by Code_Nashor · May 11, 2015 at 12:29 PM
You can't access out-of-the-box C#-Scripts from JS-Scripts or converse, they have a different compile time at project start.
Look: http://answers.unity3d.com/questions/252865/accessing-a-c-field-from-javascript.html
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making changes to a terrian in game 1 Answer
Transforms are SUPER HARD! 2 Answers
How to make method that gets multiple types? 1 Answer
Destroy object with more clicks depending on Scale? 0 Answers