- Home /
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?
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.
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.
Well, my prefab has a rigidbody and the inspector shows that the Grenade prefab is assigned to the Shoot script
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"?
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.
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.
it says by me : A local variable named `clone' is already defined in this scope when i put the code from Warwick
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!!!