- Home /
Is it possible to assign the script for GetComponent in the editor?
Hello, i trying to make, that i can go into different types of vehicles(planes, ships ,etc.). But i have one problem. I try to change the scriptname, which get activated in the editor I tryed this, but i didnt work. Does Anyone know how to realise this?
My code:
public Transform Vehicle;
public string scriptname = "SpaceShipControll";
Vehicle.GetComponent(scriptname).enabled = true;
Answer by DaveA · Aug 10, 2013 at 06:21 PM
Actually you can: http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx
Vehicle.GetComponent(Type.GetType("SpaceShipControll")); // should work
The $$anonymous$$SDN is a gold $$anonymous$$e. Of course, not all of it is in $$anonymous$$ono, but a lot of handy stuff!
Wait, now i get errors. Assets/Scripts/VehicleEnterExit.cs(69,50): error CS0103: The name `Type' does not exist in the current context.
Do i need any other usings than using UnityEngine and using System.Collections; ?
Vehicle.GetComponent(Type.GetType(scriptname));
no errors but i cannot set enabled in the same line to false like in :
Vehicle.GetComponent("scriptname").enabled = true;
$$anonymous$$y guess is the GetComponent returns a Component-type; not the type of your specific component.
Try recasting the result before accessing its variables
like this:
( (Type.GetType(scriptname)) Vehicle.GetComponent(Type.GetType(scriptname))).enabled = true;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how toDisable the FPSInputController. 1 Answer