Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Multiplayer FPS - Error when adding force to the player if they were close to a rocket explosion

Discussion in 'Scripting' started by 420BlazeIt, Jan 16, 2015.

  1. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I am making a multiplayer FPS. Today I wanted to make it so if a rocket lands near you then you jump into the air.

    The rocket instantiates an explosion particle prefab wherever it hits and the particle prefab has a box collider. The box collider is trigger. You will see in my script that I wanted the player to jump into the air when he/she enters the explosion's box collider.

    The error:
    NullReferenceException: Object reference not set to an instance of an object
    HealthAndDamage.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Scripts/HealthAndDamage.cs:206)

    The error comes from line 206 but I don't see any problem with that line.
    If I run the server in Unity and connect to the server in a built window of the game, when I am in the "SplashDamage" tag this error appears. The player is not getting propelled into the air like I want it to.

    The script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /// <summary>
    5. /// This script is attached to the Trigger GameObject on the player and
    6. /// it manages the health of the player across the network and applies
    7. /// damage to the player across the network.
    8. ///
    9. /// This script accesses the PlayerDatabase script to check the PlayerList.
    10. ///
    11. /// This script accesses the SpawnScript to inform the player that they've
    12. /// been destroyed.
    13. ///
    14. /// This script accesses the CombatWindow script to tell to add a new entry.
    15. ///
    16. /// This script is accessed by the BlasterScript.
    17. ///
    18. /// This script is accessed by the StatDisplay script.
    19. ///
    20. /// This script is accessed by the PlayerLabel script.
    21. ///
    22. /// </summary>
    23.  
    24. public class HealthAndDamage : MonoBehaviour {
    25.    
    26.     //Variables Start___________________________________
    27.    
    28.     private GameObject parentObject;
    29.    
    30.    
    31.     //Used in figuring out on who's computer the damage should be applied.
    32.    
    33.     public string myAttacker;
    34.    
    35.     public bool iWasJustAttacked;
    36.    
    37.    
    38.     //These variables are used in figuring out what the player has been hit
    39.     //by and how much damage to apply.
    40.    
    41.     public bool hitByBlaster = false;
    42.  
    43.     private int blasterDamage = 100;
    44.    
    45.  
    46.    
    47.     //This is used to prevent the player from getting hit while they are
    48.     //undergoing destruction.
    49.    
    50.     private bool destroyed = false;
    51.    
    52.    
    53.     //These variables are used in managing the player's health.
    54.    
    55.     public float myHealth = 100;
    56.    
    57.     public float maxHealth = 100;
    58.    
    59.     private float healthRegenRate = 1.3f;
    60.    
    61.     public float previousHealth = 100;
    62.    
    63.        
    64.     //Variables End_____________________________________
    65.    
    66.    
    67.     // Use this for initialization
    68.     void Start ()
    69.     {
    70.         //The trigger GameObject is used in hit detection but it is
    71.         //the parent that needs to be destroyed if the player's health
    72.         //falls below 0.
    73.  
    74.         if(networkView.isMine) {
    75.        
    76.             parentObject = transform.parent.gameObject;
    77.  
    78.         } else {
    79.  
    80.             enabled = false;
    81.  
    82.         }
    83.     }
    84.    
    85.     // Update is called once per frame
    86.     void Update ()
    87.     {
    88.         //If the player is hit by an opposing team projectile,
    89.         //then that projectile will have set hitJustAttacked to true.
    90.        
    91.         if(iWasJustAttacked == true)
    92.         {
    93.             GameObject gameManager = GameObject.Find("GameManager");
    94.            
    95.             PlayerDatabase dataScript = gameManager.GetComponent<PlayerDatabase>();
    96.            
    97.            
    98.             //Sift through the player list and only carry out hit detection if the
    99.             //attacking player is the one running this game instance.
    100.            
    101.             for(int i = 0; i < dataScript.PlayerList.Count; i++)
    102.             {
    103.                 if(myAttacker == dataScript.PlayerList[i].playerName)
    104.                 {
    105.                     if(int.Parse(Network.player.ToString()) == dataScript.PlayerList[i].networkPlayer)
    106.                     {
    107.                         //Check what the player was hit by and apply damage.
    108.                        
    109.                         if(hitByBlaster == true && destroyed == false)
    110.                         {
    111.  
    112.                             myHealth = myHealth - blasterDamage;  
    113.                            
    114.                             //Send out an RPC so that this player's attacker is
    115.                             //updated across the network. This way the attacker
    116.                             //can receive a score for destroying the enemy player.
    117.                            
    118.                             networkView.RPC("UpdateMyCurrentAttackerEverywhere",
    119.                                             RPCMode.Others, myAttacker);
    120.                            
    121.                            
    122.                             //Send out an RPC so that this player's health is reduced
    123.                             //across the network.
    124.                            
    125.                             networkView.RPC("UpdateMyCurrentHealthEverywhere",
    126.                                             RPCMode.Others, myHealth);
    127.                         }
    128.                     }
    129.                 }
    130.             }
    131.            
    132.             iWasJustAttacked = false;
    133.            
    134.         }
    135.        
    136.        
    137.         //Each player is responsible for destroying themselves.
    138.        
    139.         if(myHealth <= 0 && networkView.isMine == true)
    140.         {  
    141.             //Access the SpawnScript and set the iAmDestroyed bool to
    142.             //true so that this player can respawn.
    143.            
    144.             GameObject spawnManager = GameObject.Find("SpawnManager");
    145.            
    146.             SpawnScript spawnScript = spawnManager.GetComponent<SpawnScript>();
    147.            
    148.             spawnScript.iAmDestroyed = true;
    149.            
    150.            
    151.            
    152.             //Remove this player's RPCs. If we didn't do this a
    153.             //ghost of this player would remain in the game which
    154.             //would be very confusing to players just connecting
    155.            
    156.             Network.RemoveRPCs(Network.player);
    157.            
    158.            
    159.             //Update the CombatWindow across the network.
    160.            
    161.             networkView.RPC("TellEveryoneWhoDestroyedWho", RPCMode.All, myAttacker, parentObject.name);
    162.            
    163.            
    164.             //Send out an RPC to destroy our player across the network.
    165.            
    166.             networkView.RPC("DestroySelf", RPCMode.All);
    167.         }
    168.        
    169.        
    170.         //If the player's health is different from their previous health then
    171.         //update the health record across the network and buffer it.
    172.        
    173.         if(myHealth > 0 && networkView.isMine == true)
    174.         {
    175.             if(myHealth != previousHealth)
    176.             {
    177.                 networkView.RPC("UpdateMyHealthRecordEverywhere", RPCMode.AllBuffered, myHealth);  
    178.             }
    179.         }
    180.        
    181.        
    182.         //Regen the player's health if it is below the max health.
    183.        
    184.         if(myHealth < maxHealth)
    185.         {
    186.             myHealth = myHealth + healthRegenRate * Time.deltaTime;
    187.         }
    188.        
    189.        
    190.         //If the player's health exceeds the max health while regenerating
    191.         //then set it back to the max health.
    192.        
    193.         if(myHealth > maxHealth)
    194.         {
    195.             myHealth = maxHealth;  
    196.         }
    197.    
    198.     }
    199.  
    200.     void OnTriggerEnter (Collider col) {
    201.  
    202.         if(col.transform.tag == "SplashDamage") {
    203.  
    204.             //networkView.RPC("ApplyBlasterForce", RPCMode.All);
    205.  
    206.             parentObject.rigidbody.AddForce(transform.up * 500);
    207.  
    208.         }
    209.  
    210.     }
    211.    
    212.    
    213.     [RPC]
    214.     void UpdateMyCurrentAttackerEverywhere (string attacker)
    215.     {
    216.         myAttacker = attacker;  
    217.     }
    218.    
    219.    
    220.     [RPC]
    221.     void UpdateMyCurrentHealthEverywhere (float health)
    222.     {
    223.         myHealth = health;  
    224.     }
    225.    
    226.    
    227.     [RPC]
    228.     void DestroySelf ()
    229.     {
    230.         Destroy(parentObject);  
    231.     }
    232.    
    233.    
    234.     [RPC]
    235.     void UpdateMyHealthRecordEverywhere (float health)
    236.     {
    237.         previousHealth = health;  
    238.     }
    239.    
    240.    
    241.     [RPC]
    242.     void TellEveryoneWhoDestroyedWho (string attacker, string destroyed)
    243.     {
    244.         //Access the CombatWindow script in the GameManager and
    245.         //tell it who destroyed who so that it appears in the combat
    246.         //window.
    247.        
    248.         GameObject gameManager = GameObject.Find("GameManager");
    249.        
    250.         CombatWindow combatScript = gameManager.GetComponent<CombatWindow>();
    251.        
    252.         combatScript.attackerName = attacker;
    253.        
    254.         combatScript.destroyedName = destroyed;
    255.        
    256.         combatScript.addNewEntry = true;
    257.     }
    258.    
    259.     /*[RPC]
    260.     void ApplyBlasterForce () {
    261.  
    262.         rigidbody.AddForce(Vector3.up * 500);
    263.  
    264.     }*/
    265.  
    266. }
    267.  
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Either parentObject isn't set, or it's set to something without a rigidbody. To find out which, add Ddbug.Log(parentObject); and Debug.Log(parentObject.rigidbody); just before line 206. Whichever is null, there's your problem.
     
  3. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Ok when I put a rigidbody on my character I fall through the floor and if I disable gravity I dont fall back down after being launched by the rocket. So basically I need gravity but it is making me fall through the floor. I tried putting a capsule collider around the player to stop from falling through the floor but that just makes movement awful. There is inbuilt gravitty in one of the First Person Controller scrips so the rigidbody should be able to work without gravity but the rigidbody seems to disable the players inbuilt colliders and gravity.

    My problem: Using gravity with the rigidbody makes me fall through the map. Not using gravity doesnt let me fall from the blast.
     
  4. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I could fix this problem if someone knew how to have a character controller and rigidbody and the rigidbody must have Use Gravity enabled without failling through floor.
     
  5. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    You need a collider on the floor, too, and your character will fall through if it starts inside the floor's collider.
     
  6. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I know.

    In my game I need to have a first person controller with a rigidbody attached. The rigidbody must have "Use Gravity" enabled.

    The only problem is that the Player falls through the floor when I am doing this.
    Any solutions?
     
  7. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    The player shouldn't fall through the floor if both have colliders and the player collider doesn't start inside the floor collider. Especially with gravity, since there's no falling without gravity. I'm having trouble understanding your problem here.

    Create a new scene. Create a cube with a collider. Create a sphere with a collider and a rigidbody, and enable Use Gravity on the rigidbody. Place the sphere a bit above the cube and hit play. The sphere shouldn't fall through the cube.

    Do you possibly have Is Kinematic enabled on your rigidbody?
     
  8. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Dude you are completely useless. I have been using Unity for 2 years. I know FFS. And why the F*** would I use Is Kinematic if this entire post is about adding force to a player? Logic.
     
    Dameon_ likes this.
  9. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    I love how grateful people are when I try to help them.

    I am the ONLY person who tried to help you with this, and I doubt anybody else will feel like being particularly helpful after seeing your response.

    Have fun with your mismanaged, cobbled together code.