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?