Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 tetsuyabh · Mar 18, 2013 at 04:27 AM · nullreferenceexception

Why can't I get my scripts to communicate without getting a NullReferenceException?

Basically, when a button is pressed, if the player is called on to do so, he should either earn points or take damage. This is my first scripting attempt, and even though things seem simple enough, I keep getting a NullReferenceException with this description:

"NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value) ButtonScript.ButtonPress (UnityEngine.GameObject target) (at Assets/Scripts/ButtonScript.js:25) ButtonScript.Update () (at Assets/Scripts/ButtonScript.js:19)"

Here is the script for player input:

 var damage:int;
 var success:int;
 var power:int;
 var buttonInput:String;
 private var playerCamera:GameObject;
 private var anxietyscript;
 private var victoryscript;
 private var buttonRing:boolean;
 
 function Awake(){
     playerCamera=GameObject.Find("PlayerCamera");
     healthscript=playerCamera.GetComponent("Health Script");
     successscript=playerCamera.GetComponent("Success Script");
     buttonRing=false;
 }
 
 function Update(){
     if (Input.GetButtonDown (buttonInput)){
     ButtonPress(gameObject);
     }
 }
 
 function ButtonPress (target:GameObject){
     if (buttonRing==false){
     playerCamera.anxietyscript.ApplyDamage=damage;
     }
     if (buttonRing==true){
     playerCamera.victoryscript.ApplySuccess=success;
     }
 }

This is one script being referenced:

 var success:int;
 var score:int;
 var max:int;
 private var successLevel:GameObject;
 
 function Awake(){
     successLevel=GameObject.Find("SuccessLevel");
 }
 
 function Start(){
     score=0;
     successLevel.guiText.text=success.ToString();
 }
 //For now we will use a guiText but will eventually enable the gauge.
 function ApplySuccess(score:int){
     score+=success;
     successLevel.guiText.text=success.ToString();
     if (score>=max){
         WinPlayer();
     }
 }
 
 function WinPlayer(){
     Application.LoadLevel("Scene-ClosingVictory");
 }

And the other:

 var health:int;
 var damage:int;
 private var healthLevel:GameObject;
 
 function Awake(){
     healthLevel=GameObject.Find("HealthLevel");
 }
 
 function Start(){
     health=100;
     healthLevel.guiText.text=health.ToString();
 }
 
 function Update(){
     if (health>200){
         health=200;
     }
 }
 //For now we will use a guiText but will eventually enable the gauge.
 function ApplyDamage(damage:int){
     health-=damage;
     healthLevel.guiText.text=health.ToString();
     if (health<=0){
         KillPlayer();
     }
 }
 
 function KillPlayer(){
     Application.LoadLevel("Scene-ClosingFail");
 }

Many thanks in advance for forgiving my amateur scripting and for not being able to find the solution by Googling it (I tried). One solution on here suggested the asker not use variables with names identical to classes, so (I am pretty sure) I didn't make that mistake. Any ideas?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Mar 18, 2013 at 05:02 AM

Start by putting a #pragma strict at the top of the file and getting it to compile. The 'healthscript' and 'successscript' variables are not defined. On line 25 and 28 you are trying to access GameObject variables that don't exist. I don't know Javascript as wall as I know C#, but I've never seen a method call through assignment like you have on these two lines either. That is call the method this way:

 anxietyscript.ApplyDamage(damage);

What you want to do in your Start() function is to initialize anxietyscript and victoryscript variables. Assuming the script names are similar to the variable names you would do:

 private var anxietyscript : AnxietyScript; 
 private var victoryscript : VictoryScript;

Then in start you would use GetComponent() to initialize them in Start. Then to use them:

 anxietyscript.ApplyDamage(damage);
Comment
Add comment · Share
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

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 2 Answers

MoveTowards doesn't work after changing tag. 2 Answers

NullPointerException while changing a variable from one script in another 1 Answer

array initialisation c# 1 Answer

Unity the NullReferenceException wizard 2 Answers


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