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 EClancy · Aug 28, 2014 at 03:40 AM · javascriptspriteobjectnullreferenceexception

Cannot access sprite of a gameobject

I am making a character selection script (similar to super smash brothers).

When the player presses Start on the controller, I instantiate a "Player1" object. When they pick a character, I would like the Player1 object to render that character's sprite. I saw a lot of other people have asked and had this question solved, but I think I'm making a silly mistake somewhere.

 var p1;
 
 function Update(){
      if ( Input.GetButtonDown("Start_1") && GlobalHandlerScript.player1Joined == false ){
          //set the "player1Joined" variable
          GlobalHandlerScript.joined("Player1");
          //instantiate player
          p1 = Instantiate(GlobalHandlerScript.player1);
      }
 
     if ( Input.GetButtonDown("A_1") && GlobalHandlerScript.player1Joined == true ){
           //find the object they selected, make it their sprite
           Debug.Log("searching for object");
           var selected = GameObject.Find("Fire Wizard");
           Debug.Log("found object, searching for sprite");
           var p1sprite = selected.GetComponent(SpriteRenderer).sprite;
           Debug.Log("found sprite, assigning sprite");
           p1.GetComponent(SpriteRenderer).sprite = p1sprite; /*BREAKS HERE*/
           Debug.Log("assigned sprite");
      } 
 }


It breaks with a NullReferenceException: Object reference not set to instance of an object.

It breaks directly after saying "searching for sprite" but before saying "found sprite", meaning it's breaking at the assignment of p1sprite. Could someone explain exactly what's causing the Null Reference Exception?

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
0
Best Answer

Answer by Kiwasi · Aug 28, 2014 at 03:58 AM

Do the GameObject found and P1 both have a SpriteRenderer?

That's the most obvious null reference here.

Comment
Add comment · Show 4 · 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 EClancy · Aug 28, 2014 at 05:47 PM 0
Share

Here's the P1 gameobject:

alt text

And here's found:

alt text

avatar image Kiwasi · Aug 28, 2014 at 06:50 PM 0
Share

Next step is to Debug.Log everything involved in the problem line to find out which is null.

 Debug.Log(selected);
 Debug.Log(selected.GetComponent(SpriteRenderer));
 Debug.Log(p1);
 Debug.Log(p1.GetComponent(SpriteRenderer));

One of these will print null to the console, identifying your error.

avatar image EClancy · Aug 29, 2014 at 07:22 PM 0
Share

p1 is null, but it was instantiated properly in the game, AND affected by other code using the same reference.

 p1 = Instantiate(GlobalHandlerScript.player1);
 p1.transform.position.x = -7;
 p1.transform.position.y = -3.5;
 p1.transform.localScale.x = .3;
 p1.transform.localScale.y = .3;

All of this code happened properly earlier in the same script. Did it somehow get cleared?

avatar image EClancy · Aug 29, 2014 at 08:12 PM 0
Share

I think I got it, it had to I misunderstood how instantiate worked, and ended up clearing p1 using another function. I didn't post it because I thought it was entirely unrelated, but thanks to those simple println statements that I really should have thought to do on my own, I followed the trail and found the problem. Thanks for the suggestion :P

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Object reference not set to an instance of an object 1 Answer

gameObject.active Find 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