Hello! Firstly I thank you in advance because I am really stuck.
I do not usually ask questions because usually I can find the solution by searching the internet.
Here is the error I get and this is my script.
NullReferenceException: Object reference not set to an instance of an object
Gun.Update () (at Assets/Gun.cs:43)
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
Vector3 position = Vector3.zero;
public Rigidbody2D projectile;
private Vector3 positionmouse = Vector3.zero;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if ((Input.touchCount > 0 &&
Input.GetTouch(0).phase == TouchPhase.Began))
{
position.x = transform.position.x;
position.y = transform.position.y;
position.z = 0;
Vector3 positiontouch = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0);
Vector3 look = Camera.main.ScreenToWorldPoint(positiontouch);
Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;
go.transform.LookAt(look);
go.AddForce(go.transform.forward * 1100);
}
if((Input.GetButtonDown("Fire1")))
{
position.x = transform.position.x;
position.y = transform.position.y;
position.z = 0;
positionmouse.x = Input.mousePosition.x;
positionmouse.y = Input.mousePosition.y;
Vector3 look = Camera.main.ScreenToWorldPoint(positionmouse);
Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;
go.transform.LookAt(look);
go.AddForce(go.transform.forward * 1100);
}
}
}
I modified my code many times to find the solution, but to no avail. The code above is the latest version, I have not been remodified.
Thank you in advance!