HI I’m new to programming and I’m trying to join a couple of scripts. the raycast part of the script seems to return an nullreferenceexception and I don’t find the solution. The code is :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TargettingSystem : MonoBehaviour {
public List <Transform> targets;
public Transform selectedUnit;
void Start()
{
targets = new List <Transform> ();
AddAllEnemies ();
selectedUnit = null;
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Tab))
{
targetEnemy ();
}
if (Input.GetMouseButtonDown(0))
{
SelectTarget ();
}
}
void SelectTarget()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 10000))
{
if (hit.transform.tag == "Enemy")
{
selectedUnit = hit.transform.gameObject.transform;
}
}
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag ("Enemy");
foreach (GameObject enemy in go)
AddTarget (enemy.transform);
}
public void AddTarget (Transform enemy)
{
targets.Add(enemy);
}
private void targetEnemy()
{
selectedUnit = targets [0];
}
}