Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Selecting an enemy using raycasthit mouseclick. Object reference not set ...

Discussion in 'Scripting' started by Coolzy, Apr 18, 2019.

  1. Coolzy

    Coolzy

    Joined:
    Mar 11, 2018
    Posts:
    3
    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
    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. public string user;
    2.     public int level;
    3.  
    4.     public float currentHP;
    5.     public float maxHP;
    6.     public float currentMP;
    7.     public float maxMP;
    8.  
    9.     public float baseDMG;
    10.     public float currentDMG;
    11.     public float baseAS;
    12.     public float currentAS;
    13.     public float baseEvade;
    14.     public float currentEvade;
    15.     public float baseCrit;
    16.     public float currentCrit;
    17.  
    18.     public float hpRegenTimer;
    19.     public float hpRegenAmount;
    20.     public float mpRegenTimer;
    21.     public float manaRegenAmount;
    22.  
    23.     public float currentXP;
    24.     public float maxXP;
    25.  
    26.     public bool isDead;
    27.  
    28.     public GameObject selectedUnit;
    29.  
    30.     public enemyStats enemyStatsScript;
    31.  
    32.     // Use this for initialization
    33.     void Start () {
    34.      
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.  
    41.         if (Input.GetMouseButtonDown(0))
    42.         {
    43.  
    44.             SelectTarget();
    45.         }
    46.  
    47.         if (Input.GetKeyDown("1"))
    48.         {
    49.             if(selectedUnit!= null)
    50.             {
    51.  
    52.                 BasicAttack();
    53.             }
    54.         }
    55.     }
    56.  
    57.     void SelectTarget()
    58.     {
    59.  
    60.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    61.         RaycastHit hit;
    62.  
    63.         if (Physics.Raycast(ray, out hit, 10000))
    64.         {
    65.  
    66.             if (hit.transform.tag == "enemy")
    67.             {
    68.  
    69.                 selectedUnit = hit.transform.gameObject;
    70.                 enemyStatsScript = selectedUnit.transform.gameObject.transform.GetComponent<enemyStats>();
    71.             }
    72.         }
    73.     }
    74.  
    75.     void BasicAttack()
    76.     {
    77.         enemyStatsScript.ReceiveDamage(10);
    78.     }
     
  2. Coolzy

    Coolzy

    Joined:
    Mar 11, 2018
    Posts:
    3
    I have fixed the error. Camera must be set to Main Camera.