public class shooting 2 : MonoBehaviour
{
public GameObject projectile;
public speed ;
// Update is called once per frame
void Update(){
}
public void shoot()
{
instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
Destroy(instantiatedProjectile, 3);
}
}
It is suposed to shot projectile on the main camera pointing direction and disappear after 3 seconds.
But i have to determinate a class to the instantiatedProjectile.
If i do this:
using UnityEngine;
using System.Collections;
public class disparar2 : MonoBehaviour
{
public Rigidbody projectile;
public float speed = 20;
// Update is called once per frame
void Update(){
}
public void shoot()
{
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
Destroy(instantiatedProjectile, 3);
}
}
When the 3 seconds pass, the instatiated object just lose the rigidbody component
That space in the class name âshooting 2â sticks out quite well.
Then, the instantiation may fail if âprojectileâ is null. Has it been specified in the editor?
If so, not sure why youâd Destroy the object immediately after creating it. Technically, itâs sound code, but you wonât see anything on the screen.
When you go to the doctor, you tell the doctor your symptoms, not just âI donât feel wellâ, so he can give a diagnosis.
What are the compilation errors?
Iâm sorry :S
Well, âThe name âinstantiatedProjectileâ does not exist in the current contextâ
I need to asign it like a GameObject or something, but i dont know what, because if i give it the GameObject class, the compilation error changes to âGameObjectâ does not contain a definition for âvelocityâ and no extension method âvelocityâ accepting a first argument of type âGameObjectâ could be found (are you missing a using directive or an assembly reference?)
I see youâve modified the original post. Thatâs a lot of valuable info.
Looks like RigidBody is a component, so you canât instantiate and cast it to a RigidBody. You have to instantiate a GameObject which has a RigidBody component on it.