- Home /
Instantiate prefab with WheelCollider. How can I do it?
I have GameObject with Collider & Rigidbody attached to it,
with 2 child GameObjects with WheelCollider attached. This child gameObject is a prefab.
This works good.
Now at runTime I want to add more prefabs to it. So I call:
GameObject go = Instantiate(prefab) as GameObject;
go.parent = rigidbodyGameObjectTransform;
But "Instantiate" will give me error even before I get chance to call "go.parent" :
WheelCollider requires an attached Rigidbody to function.
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
Answer by mustafa · Aug 27, 2015 at 01:26 PM
Thanks to everyone who tried to help me.
But I did solved it now. I need to set wheelCollider.enabled = false;
before Instantiate, Instantiate and then set it back on with wheelCollider.enabled = true;
So I don't get the error with "WheelCollider requires an attached Rigidbody to function"
Answer by some_developer · Aug 27, 2015 at 12:25 PM
hey, i'm not to sure what your asking but here is my answer if you want to instatiate a prefab with a wheel collider you need to make an object such as a cube. After you make an object you need to give it a wheel collider by pressing the add component button and typing in collider. When you find the collider you want you need to save it as a prefab. Then make a new JS code and add this code:
#pragma strict
var prefab = GameObject;
function Start () {
Instantiate(prefab, new Vector3.zero, Quaternion.identity);
}
now make a new empty game object and assign the script to it. Finally in the inspector assign the prefab of the item your trying to instantiate to prefab and your done. Your prefab will spawn on the empty gameobject. If you want to spawn it at a specific location change the code to this
#pragma strict
var prefab = GameObject;
function Start () {
Instantiate(prefab, new Vector3 ([x-axis here], [y-axis here], [z-axis here]), Quaternion.identity);
}
if you had any problems please reply
Thanks. This is what I'm doing exactly: I create prefab with wheelCollider attached to it. Now when I Instantiate it like you did I get this error:
WheelCollider requires an attached Rigidbody to function. UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
This is because wheelCollider requires that there is a rigidbody in a parent object. So I can't add wheels to existing car at runtime. Try it :)
Answer by IgorAherne · Aug 27, 2015 at 12:37 AM
When you create a prefab, you take a snapshot of the objects' hierarchy, to re-use and replicate later.
-parentObj
-ChildObj
-GrandChildObj
Thus, when you select ChildObj and create a prefab out of it, you create a snapshot of:
-ChildObj
-GrandChildObj
So I advise you to create a prefab by selecting parentObj, that way you will be able to see children. This will require you to re-direct several functions to point at to a different place (downwards from parent, or something which suits you).
Thanks, but don't want the full car to be a prefab because I want to add wheels to the car at run time.
Your answer
Follow this Question
Related Questions
Getting instance of an sub object rather than the original's subobject 0 Answers
Car physics without wheel colliders? I've been trying for ages. 1 Answer
How to Instantiate prefab as child? (Java) 1 Answer
Renaming Child Objects during Instantiate? 1 Answer
Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer