- Home /
Instantiating a car prefab and adding wheel colliders to it
I am creating a car sumulation that has cars drive down a road toward a player. At the moment I have a car model prefab that I drag and drop into the scene. There is a script attached to that model that has a path and wheel collider variables, in the inspector I can drag and drop these path and collider variables onto the prefab. doing this everything works fine and the car drives down the road following the path.
What I want to do now is instanciate the prefab and attach the path and wheel collider variables to the car prefab within another "GameMaster" script, I have some code but am a bit lost, the car gets instanciated but hasn't got a path or wheel colliders attached so it doesn't move. I am a noob and would really appericate any help.
var craClone : GameObject;
var path : GameObject;
carClone = Instantiate(car);
path = GameObject.Find("Path");
Debug.Log(path);
carClone.path = path;
//WheelFR = GameObject.Find("/car(Clone)/RivalCar/WheelColliders/WheelFR");
Okay well for one, craClone should be carClone, line 1 Where is your car variable co$$anonymous$$g from? What output does path give?
sorry that was a typo. I am declaring a variable carClone as a GameObject on line 1 and then on line 3 instantiating car which is a prefab of a car model containing wheel colliders and storing that in carClone. the output of the Debug.Log(path) is Path(UnityEngine.GameObject) UnityEngine.Debug:Log(Object). The path variable is created with this script
//creating array called path
var path : Array;
//creating a variable rayColor and setting it equal to white
var rayColor : Color = Color.white;
//calling OnDrawGizmos function
function OnDrawGizmos(){
//setting Gizmos color to white
Gizmos.color = rayColor;
//creating an array containing all of the path child objects as transforms
var path_objs : Array = transform.GetComponentsInChildren(Transform);
path = new Array();
//I DONT $$anonymous$$NOW FIND OUT
for (var path_obj : Transform in path_objs){
if (path_obj != transform)
path [path.length] = path_obj;
}
//I DONT $$anonymous$$NOW FIND OUT
for (var i: int = 0; i < path.length; i++){
var pos : Vector3 = path[i].position;
if(i>0){
var prev = path[i-1].position;
Gizmos.DrawLine(prev,pos);
Gizmos.DrawWireSphere(pos,0.3);
}
}
}
The path variable should be used by the car as a path to follow.
Thank you for the reply and sorry if my question isn't clear.
Your answer
![](https://koobas.hobune.stream/wayback/20220613172410im_/https://answers.unity.com/themes/thub/images/avi.jpg)