0

Here is my script please help me fix the error. It is for my unity game, the script is to shoot in my wave spawner game. I am using C#.

using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour {

    public float fireRate = 0;
    public float Damage = 10;
    public LayerMask whatToHit;

    float timeToFire = 0;
    Transform firePoint;

    void Awake() {
        firePoint = transform.FindChild("Firepoint");
        if (firePoint == null) {
            Debug.LogError("No firepoint? WHAT?!");
        }
    }

    void Update()
    {
        Shoot();
        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, whatToHit);
        Debug.DrawLine (firePointPosition, (mousePosition - firePointPosition) * 100, Color.cyan);
        if (hit.collider != null)  {
            Debug.DrawLine (firePointPosition, hit.point, Color.red);
            Debug.Log ("We hit" + hit.collider.name + " and did " + Damage + "damage.");
        }
    }
}
J...
  • 30,968
  • 6
  • 66
  • 143
  • I do not understand that post which is why i made my own thread. – Iam Destroyer Sep 27 '16 at 00:24
  • 1
    That does not make it any less of a duplicate. What you need to do is spend some serious time ***debugging*** your application. Line by line, particularly in the area flagged by the Stack Trace. Learning how to debug is an invaluable skill. – David L Sep 27 '16 at 00:26
  • 1
    @IamDestroyer Keep reading it until you understand. This is important. At very least, if you want help, we need the complete details of the error, including the line number where it happened and the offending object. – J... Sep 27 '16 at 00:26
  • What have you referenced in your inspector to the LayerMask whatToHit? – Alan-Dean Simonds Sep 27 '16 at 00:32

0 Answers0