Hi! Need help. Im making a rpg and i made a C# script.
So i can mark which npc i clicked on. But the problem is the diselect thing.
Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NpcTargeting : MonoBehaviour
{
public List<Transform> targets = new List<Transform>();
public Transform selectedTarget;
public GameObject myTransform;
public RaycastHit hit = new RaycastHit();
public Ray ray;
// Use this for initialization
void Start ()
{
selectedTarget = null;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
selectedTarget = hit.transform;
targets.Add(selectedTarget);
myTransform = hit.transform.gameObject;
}
}
if(selectedTarget != null)
{
selectedTarget.Find("Graphic/NpcCircle").gameObject.SetActive(true);
}
if(selectedTarget == null)
{
if(targets.Count == 0)
{
myTransform.transform.Find("Graphic/NpcCircle").gameObject.SetActive(false);
}
}
if(targets.Count == 2)
{
targets.Clear();
myTransform.transform.Find("Graphic/NpcCircle").gameObject.SetActive(false);
selectedTarget = null;
}
}
}
I’ll get an error where it says “UnassignedReferenceException: The variable myTransform of NpcTargeting has not been assigned.”