Search Unity

Shoot problem

Discussion in 'Scripting' started by mwg3, Apr 20, 2018.

  1. mwg3

    mwg3

    Joined:
    Apr 8, 2018
    Posts:
    36
    Hi, i have problem..
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class Enemy : NetworkBehaviour {
    5.  
    6.     [SerializeField]
    7.     private int health = 100;
    8.     [SyncVar]
    9.     private int currentHealth;
    10.     string _netID = System.Guid.NewGuid().ToString();
    11.  
    12.     void Awake()
    13.     {
    14.  
    15.         GameManager.RegisterEnemy(_netID, this);
    16.     }
    17.  
    18.     public void TakeDamage(int _amount, string _enemyID)
    19.     {
    20.         if (!isServer)
    21.         {
    22.             return;
    23.         }
    24.         currentHealth -= _amount;
    25.         Debug.Log("He has " + _enemyID + " hp: " + currentHealth);
    26.  
    27.         if (currentHealth <= 0)
    28.         {
    29.             Debug.Log("This mob is dead!");
    30.         }
    31.     }
    32.  
    33. }
    34.  
    Code (CSharp):
    1.     private static Dictionary<string, Enemy> enemies = new Dictionary<string, Enemy>();
    2.  
    3.     public static void RegisterEnemy(string _netID, Enemy _enemy)
    4.     {
    5.         string _enemyID = "Enemy" + _netID;
    6.         enemies.Add(_enemyID, _enemy);
    7.         _enemy.transform.name = _enemyID;
    8.     }
    9.  
    10.     public static void UnRegisterEnemy(string _enemyID)
    11.     {
    12.         enemies.Remove(_enemyID);
    13.     }
    14.  
    15.     public static Enemy GetEnemy(string _enemyID)
    16.     {
    17.         if (!enemies.ContainsKey(_enemyID)){
    18.         Debug.Log("This enemy id is no good! " + _enemyID);
    19.             return null;
    20.         }
    21.         return enemies[_enemyID];
    22.     }
    Code (CSharp):
    1.     [Client]
    2.     void Shoot()
    3.     {
    4.  
    5.         RaycastHit _hit;
    6.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range))
    7.         {          
    8.             if (_hit.collider.tag == "Enemy"){
    9.              CmdPlayerShot(_hit.collider.name, weapon.damage);
    10.            }
    11.         }
    12.  
    13.     }
    14.    
    15.     [Command]
    16.     void CmdPlayerShot(string _enemyID, int _damage)
    17.     {
    18.         Debug.Log(_enemyID + " has been shot.");
    19.         Enemy enemy = GameManager.GetEnemy(_enemyID);
    20.         enemy.TakeDamage(_damage, _enemyID);
    21.  
    22.     }
    When the client(not host) wants to attack the monster shows up
    "NullReferenceException: Object reference not set to an instance of an object
    PlayerShoot.CmdPlayerShot (System.String _enemyID, Int32 _damage) (at Assets/Assets/Scripts/Player/PlayerShoot.cs:58)"
     
  2. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    The null ref is on the line Enemy enemy = GameManager.GetEnemy(_enemyID);?
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It would help if you would say what line the error is pointing to, since you didn't include the whole script (so we can't tell what line is what).

    GameManager's reference might not be set, or GameManager.GetEnemy might be returning null. Add a check at the start of CmdPlayerShot to check if GameManager is null or not. Add a check after you call GameManager.GetEnemy to see if the returned enemy is null or not.
     
  4. mwg3

    mwg3

    Joined:
    Apr 8, 2018
    Posts:
    36
    When i put enemy on map only the host can shoot him.. i dunno why..
    When the client joins the server, he can not attack him because "NullReferenceException: Object reference not set to an instance of an object
    PlayerShoot.CmdPlayerShot" :(
     
  5. mwg3

    mwg3

    Joined:
    Apr 8, 2018
    Posts:
    36
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hey there, does this print in the console? "Debug.Log("This enemy id is no good! " + _enemyID);"