where errors

I’m from turkey and I can’t speak english.I am do a 2.5d game but I Can’t rotate a gun.This my script and error:

using UnityEngine;
using System.Collections;
public class silahhareket : MonoBehaviour {
    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {
        //rotation
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 5.42f;
      
        Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
        mousePos.x = mousePos.x - objectPos.x;
        mousePos.y = mousePos.y - objectPos.y;
      
        float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    }
}

error:
NullReferenceException: Object reference not set to an instance of an object
silahhareket.Update () (at Assets/silah ayarları/silahhareket.cs:17)

and I have a different scripts it’s a fire scripts But not a cube fire because I can’t find the cube fire gun(if you know a cube fire gun please write the scripts):

using UnityEngine;
using System.Collections;

public class fire : MonoBehaviour {

    public float fireRate = 0;
    public float damage = 10;
    public LayerMask notToHit;

    float timeToFire = 0;
    Transform firePoint;

    // Use this for initialization
    void Awake () {
        firePoint = transform.FindChild ("FirePoint");
        if (firePoint == null) {
            Debug.LogWarning ("No firePoint? What?!");
        }
    }
    // Update is called once per frame
    void Update () {
        if (fireRate == 0) {
                        if (Input.GetButtonDown ("Fire1")) {
                            shoot();
                        }
                } else {
            if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
                timeToFire = Time.time + 1/fireRate;
                shoot();
            }
        }
    }
    void shoot () {
        Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
        Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
        RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, notToHit);
        Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
        if (hit.collider != null) {
            Debug.Log ("test");
            Debug.DrawRay (firePointPosition, hit.point, Color.red);
            }
        }
}

error:

NullReferenceException: Object reference not set to an instance of an object
fire.shoot () (at Assets/silah ayarları/fire.cs:35)
fire.Update () (at Assets/silah ayarları/fire.cs:25)

(Sorry I can some speak english)

Use the Insert Code button and check indentation, please

what is this?

When you edit a message, the button between the film icon and the floppy disk icon serve to paste code like this:

if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
    timeToFire = Time.time + 1/fireRate;
    shoot();
}

please help meee

The first script works with no error. The seconds works too but you have to add an empty gameObject called “FirePoint” as a child representing the weapon position.

scripts not work.
first scripts error:
NullReferenceException: Object reference not set to an instance of an object
silahhareket.Update () (at Assets/silah ayarları/silahhareket.cs:17)

second scripts error:
NullReferenceException: Object reference not set to an instance of an object
fire.shoot () (at Assets/silah ayarları/fire.cs:35)
fire.Update () (at Assets/silah ayarları/fire.cs:25)

For the first script you need at least one camera in the scene with tag “MainCamera”.
For the second script:

  • From the menu: GameObject\Create Empty
  • rename the GameObject “FirePoint”.
  • Drag the FirePoint on the object where the Fire.cs is attached.

If you still see in the log “No firePoint? What?!” then you did something wrong in the 3 points above.

Not Work again :frowning:

Can you check what are the lines 17 and 35 please? Are exactly the same as in your first post?
This is 17? mousePos.y= mousePos.y- objectPos.y;
This is 35? Vector2 firePointPosition =newVector2(firePoint.position.x, firePoint.position.y);

The only thing that give me an error is if I change the MainCamera Tag to something else than “MainCamera”. So double check in the inspector that all your Cameras have the correct tag.
Other than that I changed the first line of shoot() because ScreenToWorldPoint need a third parameter too (distance from camera). Otherwise return always the same value.

    void shoot () {
        Vector3 mp = Input.mousePosition;
        mp.z = 10; // z = Distance from the camera
        Vector2 mousePosition = Camera.main.ScreenToWorldPoint (mp);
        Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
        RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, notToHit);
        Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
        if (hit.collider != null) {
            Debug.Log ("test");
            Debug.DrawRay (firePointPosition, hit.point, Color.red);
        }
    }

Not work this is the pictures:

You have no variable declarations for firePoint or notToHit. It looks like you’re literally copy-pasting code from the forum into your script and expecting it to work, which it rarely does - as you’ve experienced. Try to debug it yourself.

I can’t do this I don’t know the code because I don’t know the english I know the turkish and I can’t find code description turkish

So you’re going to rely on the folks in this forum to write the code for your game? I understand the frustrations of being an ESL programmer but come on now…

I gave you the part I have changed but of course you have to include it into what you already have…
And that was in the fire.cs!

I forget what. How do you texture?