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
1
Question by Patriotusko · Jul 06, 2011 at 12:10 PM · c#rigidbodyinstantiate

Instantiate rigidbody C#

There is an example from Unity Scripting:

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
 
     public Rigidbody projectile;
     void Update() {
         if (Input.GetButtonDown("Fire1")) {
             Rigidbody clone;
             clone = Instantiate(projectile, transform.position, transform.rotation);
             clone.velocity = transform.TransformDirection(Vector3.forward * 10);
         }
     }
 }

and that's my code:

 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
     public double Speed = 3.0;
     public Rigidbody grenadePrefab;
         void Start () {
     
     }
     
     void Update () {
         if (Input.GetButtonDown("Fire1"))
         {
         
             if (Collisions.GRENADE_AMMO > 0)
             {
                 Rigidbody grenade = (Rigidbody)Instantiate(grenadePrefab, transform.position, Quaternion.identity);
                 grenade.rigidbody.AddForce(Vector3.up * 2000, ForceMode.Impulse);
                 Collisions.GRENADE_AMMO -=1;
                 print(Collisions.GRENADE_AMMO);
             }
         }
     
     }
 }

When using the example, it says, it needs a cast But after adding a cast in both cases occures an error

InvalidCastException: Cannot cast from source type to destination type. Shoot.Update () (at Assets/Scripts/Shoot.cs:19)

Can anyone please tell me, what the problem is?

Comment
Add comment · Show 9
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 asafsitner · Jul 06, 2011 at 12:23 PM 0
Share

Why don't you attach the Rigibody Component to the prefab? You can do that through script with the gameObject.AddComponent("Rigidbody"); But in this case using the editor will be much more efficient, imo.

avatar image Bunny83 · Jul 06, 2011 at 12:28 PM 0
Share

Actually you can't even drag & drop a prefab without a Rigidbody onto the prefab-variable. So you either forgot to add a Rigidbody to your prefab, or you just don't assigned something to the variable.

avatar image Patriotusko · Jul 06, 2011 at 12:36 PM 0
Share

Well, my prefab has a rigidbody and the inspector shows that the Grenade prefab is assigned to the Shoot script

avatar image Bunny83 · Jul 06, 2011 at 12:51 PM 1
Share

I've tested your code and it works like it should. I had to replace your Collisions.GRENADE_A$$anonymous$$$$anonymous$$O variable with a normal local variable since i don't have your Collisions class.

If i click, it "shoots" my prefab straight up. I don't have compiler errors nor runtime errors. Are you sure you don't have one of your own class called "Rigidbody"?

avatar image Patriotusko · Jul 06, 2011 at 01:04 PM 0
Share

No, i only have 3 classes so far and those are "Collisions, Shoot and Respawn";

If I click, it "shoots" too; but: 1. It doesn't react on force changes; 2. It ignores the If statement and makes as many grenades as i want ins$$anonymous$$d of making only 8 of them.

I've tried doing it like that: "public Rigidbody grenadePrefab;" and there are neither errors nor warnings, but i just can't see the grenades i shoot. But it shoots only 8 of them as it has to be.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Waz · Jul 06, 2011 at 10:44 PM

The code seems to instantiating the Rigibody rather than the GameObject containing it. I think it should be:

 var clone = Instantiate(projectile.gameObject, ...);
 clone.rigidbody.velocity = ...

Your version is actually half doing that already.

Comment
Add comment · Show 3 · 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 merlinwoff · Feb 15, 2013 at 03:36 PM 0
Share

it says by me : A local variable named `clone' is already defined in this scope when i put the code from Warwick

avatar image seansteezy · Jul 17, 2014 at 04:52 PM 0
Share

Thanks @Waz, that was my problem when I came across this issue, I was adding forces to the object used to create the instance, not the result of my instantiation... Thanks for your wisdom!!!

avatar image BobTheZealot · Oct 26, 2014 at 05:16 PM 0
Share

Why are you using javascript on a c# question?

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

8 People are following this question.

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

Related Questions

Instantiated projectile always floats up 1 Answer

AddForce NullReferenceException 1 Answer

Instantiate Problem 1 Answer

How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 1 Answer

Projectile not moving properly in top down 2d shooter 2 Answers


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