Hey, I’m having an issue with my target selection system… It should assign the clicked object to the gameobject ‘selectedUnit’ and my aim is to make a mmorpg-style selection system.
I have a box collider and the tag ‘enemy’ set inside the unity editor, also there is a rigidbody. I have tried to set it to ‘trigger’ as well, but it seems like it didn’t do much. Please help. The ‘enemy’ is a typical 3d cube object.
Error: [10:58:54] NullReferenceException: Object reference not set to an instance of an object. playerStats.SelectTarget () at Assets/Scripts/newScripts/playerStats.cs:66
When doubleclicking the error it is in the Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
part. Below find my whole script. Thanks
Please help, thanks a lot, I appreciate it.
Here is my code:
public string user;
public int level;
public float currentHP;
public float maxHP;
public float currentMP;
public float maxMP;
public float baseDMG;
public float currentDMG;
public float baseAS;
public float currentAS;
public float baseEvade;
public float currentEvade;
public float baseCrit;
public float currentCrit;
public float hpRegenTimer;
public float hpRegenAmount;
public float mpRegenTimer;
public float manaRegenAmount;
public float currentXP;
public float maxXP;
public bool isDead;
public GameObject selectedUnit;
public enemyStats enemyStatsScript;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
SelectTarget();
}
if (Input.GetKeyDown("1"))
{
if(selectedUnit!= null)
{
BasicAttack();
}
}
}
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;
enemyStatsScript = selectedUnit.transform.gameObject.transform.GetComponent<enemyStats>();
}
}
}
void BasicAttack()
{
enemyStatsScript.ReceiveDamage(10);
}