Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mustafa · Aug 26, 2015 at 10:05 PM · instantiateparentwheelcolliderwheel collider

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)




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mustafa · Aug 27, 2015 at 12:40 PM 0
Share

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 :)

avatar image
0

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mustafa · Aug 27, 2015 at 08:45 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges