I dont know how to solve this [noob problem]

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

You didn’t tell us what the problem is!

What is it doing? What do you want it to do?

Shoot a projectile and it should disappear after 3 seconds.

I’ll venture some guesses.

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.

Ah, never used the timer version of Destroy().

Well, now we know what you want it to do. How about “what is it doing?”

Compilation errors :S

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?

1 Like

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.

Then, you can call

RigidBody rb = instantiatedObject.GetComponent<RigidBody>();
if (rb != null)
{
///rb.velocity = ...
}
1 Like

Solution:

Make it as a Rigidbody, and then make a script on the Bullet to destroy it after 3 seconds:

using UnityEngine;
using System.Collections;

public class DestroyBullet : MonoBehaviour
{

    void Start()
    {
        Destroy(gameObject, 3f);
    }
}

Definitely a way to do it.