Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by panorama · Sep 17, 2011 at 06:24 AM · iosnetwork

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!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image panorama · Sep 17, 2011 at 10:07 AM 0
Share

anybody help!

avatar image Ray-Pendergraph · Sep 17, 2011 at 01:24 PM 0
Share

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.

avatar image panorama · Sep 17, 2011 at 04:08 PM 0
Share

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;
 }
avatar image Ray-Pendergraph · Sep 17, 2011 at 11:42 PM 0
Share

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

avatar image Bovine · Sep 28, 2011 at 07:48 PM 0
Share

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).

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

other players and the server does not receive networkView.RPC 1 Answer

Damage does not receive on my Tank[Network] 1 Answer

Make the first person controller transparent? 1 Answer

How can i find to Cool Main menu for iOS 1 Answer

How to use option setting Vibrate and Mute 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges