Beginner Issues - Debug Simple Shooting Game Code

Just started with Unity and already stuck : /. I highlighted the rows and included the error messages. Any help or guidance much appreciated!

using UnityEngine;
using System.Collections;

public class Shooter : MonoBehaviour {
    public Rigidbody bullet;
    public float power = 1500f;
    public float moveSpeed = 2f;

    void Update () {
        var h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
        transform.Translate (h, v, 0);
        if(Input.GetButtonUp ("Fire1")) {
            Rigidbody instance = Instantiate(bullet, Transform.position, Transform.rotation) as Rigidbody;
            Vector3 fwd = Transform.TransformDirection(Vector3.forward);
            instance.AddForce (fwd * power);
    }
    }
}

Error Messages
Line: Rigidbody instance = Instantiate(bullet, Transform.position, Transform.rotation) as Rigidbody;
Assets/Scripts/Shooter.cs(14,76): error CS0120: An object reference is required to access non-static member UnityEngine.Transform.position'* *&* *Assets/Scripts/Shooter.cs(14,96): error CS0120: An object reference is required to access non-static member UnityEngine.Transform.rotation’
&
Assets/Scripts/Shooter.cs(14,46): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments* *&* *Assets/Scripts/Shooter.cs(14,46): error CS1503: Argument #2’ cannot convert object' expression to type UnityEngine.Vector3’

*Line: Assets/Scripts/Shooter.cs(15,49): error CS0120: An object reference is required to access non-static member UnityEngine.Transform.TransformDirection(UnityEngine.Vector3)'** *Assets/Scripts/Shooter.cs(15,49): error CS0120: An object reference is required to access non-static member UnityEngine.Transform.TransformDirection(UnityEngine.Vector3)’

Code tags are your friend. I would instantiate a GameObject and not the Rigidbody.

instance.AddForce (instance.transform.forward* power);
1 Like

also you are using Transform in your instantiation code for position and rotation. you want the lower case transform. Transform is the class and transform is an instance of that class.

2 Likes

Thanks, alot for your help. lower case and higher case was key :).