Search Unity

I dont know how to solve this [noob problem]

Discussion in 'Scripting' started by qiqette, Feb 8, 2016.

  1. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Code (CSharp):
    1. public class shooting 2 : MonoBehaviour
    2. {
    3.     public GameObject projectile;
    4.     public  speed ;
    5.  
    6.     // Update is called once per frame
    7.     void Update(){  
    8.      
    9.     }
    10.     public void shoot()
    11.     {
    12.         instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation);
    13.         instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
    14.         Destroy(instantiatedProjectile, 3);
    15.     }
    16. }
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class disparar2 : MonoBehaviour
    5. {
    6.     public Rigidbody projectile;
    7.     public float speed = 20;
    8.  
    9.     // Update is called once per frame
    10.     void Update(){    
    11.      
    12.     }
    13.     public void shoot()
    14.     {
    15.         Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
    16.         instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
    17.         Destroy(instantiatedProjectile, 3);
    18.     }
    19. }
    When the 3 seconds pass, the instatiated object just lose the rigidbody component
     
    Last edited: Feb 8, 2016
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    You didn't tell us what the problem is!

    What is it doing? What do you want it to do?
     
  3. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Shoot a projectile and it should disappear after 3 seconds.
     
  4. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    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.
     
  5. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Ah, never used the timer version of Destroy().
    Well, now we know what you want it to do. How about "what is it doing?"
     
  6. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Compilation errors :S
     
  7. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    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?
     
    qiqette likes this.
  8. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    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?)
     
  9. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    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
    Code (CSharp):
    1. RigidBody rb = instantiatedObject.GetComponent<RigidBody>();
    2. if (rb != null)
    3. {
    4. ///rb.velocity = ...
    5. }
     
    qiqette likes this.
  10. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Solution:

    Make it as a Rigidbody, and then make a script on the Bullet to destroy it after 3 seconds:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyBullet : MonoBehaviour
    5. {
    6.  
    7.     void Start()
    8.     {
    9.         Destroy(gameObject, 3f);
    10.     }
    11. }
     
  11. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Definitely a way to do it.