- Home /
NullReferenceException: Object reference not set to an instance of an object
Hello i’m used pickup prefab in M2H network tutorial example 4 but when i keep it in game my console of Unity warned NullReferenceException: Object reference not set to an instance of an object
Pickup.Pickup (UnityEngine.GameObject object, .PlayerScript playerScript) (at Assets/Resources/GameAssets/Scripts/Pickup.js:49) Pickup.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Resources/GameAssets/Scripts/Pickup.js:44) And i observed in Tutorial PickupPrefab was red color in scene but my scene was blue How can I resolved this problem
/ This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl) The original author of this code is Mike Hergaarden, even though some small parts are copied from the Unity tutorials/manuals. Feel free to use this code for your own projects, drop us a line if you made something exciting! /
pragma implicit
pragma downcast
var ammoGO : GameObject; var powerupGO : GameObject; var hpGO : GameObject;
public var powerupText : GUIText; var respawnTimePickup : int =30;
private var isActive : boolean = false; private var powerUps = new Array(); private var currentItem : int =0; private var currentGO : GameObject;
function Start(){ powerupText = GameObject.Find("powerupGUIText").GetComponent(GUIText); powerupText.text="";
powerUps.Add("Invincible");
powerUps.Add("hp");
powerUps.Add("ammo");
hpGO.SetActiveRecursively(false);
ammoGO.SetActiveRecursively(false);
powerupGO.SetActiveRecursively(false);
NewPickup(0, Random.Range(0,powerUps.length));
}
function OnTriggerEnter (other : Collider) { var object = other.gameObject; var playerScript : PlayerScript = object.GetComponent(PlayerScript);
if(isActive){
Pickup(object, playerScript);
}
}
function Pickup(object : GameObject, playerScript : PlayerScript){
if(!playerScript.localPlayer){
//Only the local player can pickup his own item
return;
}
var pickedUp : String =powerUps[currentItem];
var playersNetworkView : uLink.NetworkView = object.GetComponent(uLink.NetworkView);
if(pickedUp=="Invincible"){
DisplayMessage("INVINCIBLE!", 10);
playersNetworkView.RPC("StartInvincibility",uLink.RPCMode.All);
}else if(pickedUp=="hp"){
var newHP=playerScript.hp+50;
if(newHP>100){
newHP=100;
}
playersNetworkView.RPC("setHP",uLink.RPCMode.All, newHP);
DisplayMessage("GOT EXTRA HP!",2);
}else if(pickedUp=="ammo"){
var gun : Machinegun = object.transform.Find("Turret Rotator/Turret/Gun Object/MG G/MG Fire").GetComponent("ShootingBullet");
gun.clips+=3;
gun.GetBulletsLeft();
DisplayMessage("GOT EXTRA AMMO!",2);
}else{
Debug.LogError("ERROR: UNKNOWN POWERUP: "+pickedUp);
}
//deactivates current pickup and will spawn a new one
GetComponent(uLink.NetworkView).RPC("NewPickup",uLink.RPCMode.All, respawnTimePickup, Random.Range(0,powerUps.length));
}
@RPC function NewPickup(seconds: int, newItem: int){ //Disable the pickup, reactive it after X seconds if(currentGO){ currentGO.SetActiveRecursively(false); } isActive=false;
yield new WaitForSeconds (seconds);
currentItem=newItem;
if(powerUps[currentItem]=="hp"){
currentGO = hpGO;
}else if(powerUps[currentItem]=="ammo"){
currentGO = ammoGO;
}else{
currentGO = powerupGO;
}
isActive=true;
currentGO.SetActiveRecursively(true);
}
function DisplayMessage(message : String, seconds : int){ powerupText.text=message; yield WaitForSeconds(seconds); if(powerupText.text==message){ powerupText.text="";
} }
Cheers!
Something is null on line 49 in the Pickup function. Since we do not have line numbers (and you do) maybe you could post line 49... also using the code tags would be helpful as unformatted code is difficult to read.
Okay I'm fix my code by the way i shown my code in line 49
function Pickup(object : GameObject, playerScript : PlayerScript){
if(!playerScript.localPlayer){
//Only the local player can pickup his own item
return;
}
The only thing I see in that block of code that can be null is playerScript. With null reference exceptions you always look to the left of the ".". In this case that's the only thing in that block. Looking at the code that would mean that there is no PlayerScript hung on the GameObject that this script is running on. Also if you are going to do a get component and you expect it to always be there you should GetComponent in the start method just one time
You probably need to drag some object onto the script. Select the GameObject you have this script on and see what public members are available and if they need anything assigning to them (which you can do be dragging and dropping the necessary GameObject, perhaps with the relevant component attached).