- Home /
Convert GameObject in CarControl
I have a script where I have a variable of type:
var Cars: CarControl;
And I have one GameObject in the Hierarchy called "PickupRed" which possesses the "CarControl" Active scripting.
How do I put the GameObject in Cars? by code?
When I try to put:
Cars = GameObject.Find("PickupRed");
says it can not convert the GameObject in CarControl.
Someone please help me.
Answer by OperationDogBird · Jan 20, 2014 at 11:39 PM
I prefer to use FindObjectOfType("CarControl") if there is only 1 in the scene, or simply just place the gameobject in question in the exposed variable.
The issue here is that you are getting the type GameObject from the Find function, but your variable is not of that type. You can use GetComponent("CarControl") directly after the find/
Cars = GameObject.Find("SomeCar").GetComponent("CarControl");
If your question is answered, please click on the checkmark on the left side of the correct answer.
BTW: I'd use the non-string version of GetComponent() ins$$anonymous$$d:
Cars = GameObject.Find("SomeCar").GetComponent(CarControl);
@robertbu its been so long since ive typed unity script i wasnt entirely sure u could pass the type. c# rox
Your answer
Follow this Question
Related Questions
Inspector Assigned Variables Not In Both Scenes 1 Answer
rigidbody2D only jump while grounded 1 Answer
How can I make a gui with buttons pop up in javasript? 1 Answer
Collision with Player and DoorTriggerLeft 0 Answers
Gun Firing help? 2 Answers