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 Fulvio13 · May 14, 2012 at 02:57 PM ·

Please help regarding NullReferenceExpection

Hello everyone, I was wondering if someone could help me on finding why I'm receiving such an error. It appears that in order to access the next level, I must press the continue button twice. Therefor I think it has to be related to score.SendMessage ("Reset");

Thanks in advance!

pragma strict

var scorePrefab: Transform;

function OnGUI () {

if (GUI.Button (Rect (540,500,200,80), "Continue")) {

 var score: GameObject = GameObject.FindWithTag("Score");
 if (!score) {
     
     GameObject.Instantiate(scorePrefab);
 }
 
 score.SendMessage ("Reset"); 
 Application.LoadLevel (3);
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Lo0NuhtiK · May 14, 2012 at 03:03 PM

I'm guessing it's not finding the score object the first time, so it's instantiating it, then you send a message to a null reference because it never found it and the thing you instantiated was never accessed by your code...
try :

 //..........//
 if(!score){
    score = instantiated score stuff ;
 }
 score.sendmessage etc..
 //....//
Comment
Add comment · Show 2 · 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
avatar image Fulvio13 · May 14, 2012 at 03:59 PM 0
Share

Thanks for the replay but this doesn't solution doesn't work, anyway I've decided to just leave the error since it doesn't effect my game.

Thanks though!

avatar image Lo0NuhtiK · May 14, 2012 at 04:56 PM 0
Share

Hope you didn't try copy/pasting the code above, obviously that wouldn't have worked.

avatar image
0

Answer by perchik · May 14, 2012 at 03:04 PM

The first thing you should try is to add some Debug.Log statements to see if you actually get anything the first time you find the score object.

My guess on what's happening is that the first time the button is pressed, there isn't actually an object with the tag "Score", so you instantiate one. The second time, after making one, there is an object with the tag "Score"

Your actual problem though, comes from the "score.SendMessage()" line, because at that point, score may exist from where you set it at the beginning, or it may not. (If you go into that if statement, score never exists, so when you call sendmessage, there's nothing to send it to.)

To fix it, just set score in the if statement:

  score = GameObject.Instantiate(scorePrefab)


(You may have to use the var score : GameObject syntax, I'm not as familiar with JS)

Comment
Add comment · Show 1 · 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
avatar image Fulvio13 · May 14, 2012 at 03:58 PM 0
Share

Hey thanks for the replay, yeah it appears that the problem does indeed come from the score.Sendmessage line, I've changed however another error pops up, anyway as long the game is functional I'm happy with it.

Thanks though!

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

6 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Any recommended books/resources on component-based design? 0 Answers

Coding with Unity 2 Answers

How do I make it so when I press a button it only triggers untill i press it again? 1 Answer

New to unity and any coding for that matter, need advice!!! 3 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