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 SuperMasterBlasterLaser · Apr 18, 2014 at 12:47 PM · gameobjectnullreferenceexceptionfindgameobjectswithtag

GetComponent NullReferenceException

Hello.

I have GameObject which got Sprite renderer and function that assigns sprite that loaded from resoures: using UnityEngine; using System.Collections;

 public class LetterButtonScript : MonoBehaviour {
 
     private int letterInt;
 
     private Sprite mySprite;
 
     private SpriteRenderer renderer;
 
     private bool dead;
     // Use this for initialization
     void Start () {
         renderer = GetComponent<SpriteRenderer>();
         letterInt = -1;
         dead = false;
     }
     
     // Update is called once per frame
     void Update () {
             
     }
     /// <summary>
     /// Sets the letter.
     /// </summary>
     /// <param name="letterNumber">Letter number.</param>
     public void setLetter(int letterNumber) {
         letterInt = letterNumber;
         mySprite = Resources.Load("letters/" + letterInt, typeof(Sprite)) as Sprite;
         renderer.sprite = mySprite;
     }
 
     public int getLetterNumber() {
         return letterInt;
     }
 
     
 }

I have onther script with function that Instansiates it:

 buttonsPositions.z = 0f;
         buttonsPositions.y = -2.7f;
         buttonsPositions.x = -4f;
         for (int i = 0; i < mysteryCharNumbers.Length; i++) {
             letterButtons[i] = Instantiate(letterButtonPrototype, buttonsPositions, this.transform.rotation) as GameObject;
             buttonsPositions.x += 0.65f;
         }

After that I try to set sprite to LetterButtons

 for (int i = 0; i < letterButtons.Length; i++) {
             LetterButtonScript script = letterButtons[i].GetComponent<LetterButtonScript>();
             script.setLetter(mysteryCharNumbers[i]);
         }
 

However, it shows me NullReferenceException and shows me setLetter function

What I did wrong?

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

Answer by SuperMasterBlasterLaser · Apr 19, 2014 at 06:26 AM

I have found why my code does not work. I have MainScript that creates LetterButtons and else. I have written Instantiate functions to create LetterButtons in Start() function.

And another function that tries to make change SpriteRenderer of LetterButtonScript also written in Start() function. So I think It tries to do all things at the same time while some components are NULL.

I solved it by creating integer variable that checks the condition:

 private int condition = -1;

In Update function it will look like that:

 void Update () {
         if (condition == I_AM_INITIALIZING_QUESTION) {
             initializeQuestion();
         } else if (condition == I_AM_INITIALIZING_LETTERBUTTONS) {
             initializeEnteringButtonsAndMysteryString();
         } else if (condition == I_AM_GIVING_VALUES_FOR_LETTERBUTTONS) {
             initializeValuesForLetterButtons();
         } else if (condition == I_AM_INITIALIZING_PLACES_FOR_LETTERS) {
             initializeLetterPlaces();    
         } else if (condition == I_AM_CHECKING_ANSWER) {
             checkAnswer();    
         }
     }
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
avatar image
0

Answer by thornekey · Apr 18, 2014 at 12:56 PM

    mySprite = Resources.Load("letters/" + letterInt, typeof(Sprite)) as Sprite;

letterInt is an int. You cant put it as a name, because it would be like a string (if u get what i mean). Im not sure but perhaps try .ToString().. Honestly i dont think that will work. but null reference exception means it doesnt exist. Perhaps put an if statement to see if it does exist first. maybe you could isolate the issue.

Comment
Add comment · Show 3 · 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 SuperMasterBlasterLaser · Apr 18, 2014 at 03:18 PM 0
Share

mySprite actually is not null, have checked. It says that renderer is null.

avatar image thornekey · Apr 18, 2014 at 04:03 PM 0
Share

hmm im not sure then :(

avatar image koray1396 · Apr 18, 2014 at 04:46 PM 0
Share

Can you try this?

 renderer = gameObject.GetComponent<SpriteRenderer>();

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnCollisionEnter GameObject throws NullReferenceException 0 Answers

HELP Find gameObject With tag in another array 1 Answer

Rigidbody: NullReferenceException: Object reference not set to an instance of an object 1 Answer

NullReferenceException: Object reference not set to an instance of an object error? 1 Answer

Instantiated cloned GameObject returns Null outside Start method 0 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