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 CatKillen · Feb 16, 2014 at 12:34 AM · nullreferenceexceptiongetcomponent

NullReferenceException when setting Component as Variable, but no bugs

This is the code I'm currently using for a flag collectible in a platformer. The stage scrolls automatically, and each time the flag collectible leaves view, it's put into the object pool (controlled by different script). When it's reenabled, it's given a new random color. The color decides which animation is used, and changes the Start Color of the particle system. The Flag is an empty game object with an Animator, a Particle System, and this script.

 using UnityEngine;
 using System.Collections;
 
 public class Flag : MonoBehaviour {
 
     private Animator animator;
     private Renderer renderer;
     private ParticleSystem particle;
 
     public int maxcolor = 4;
     private Color myyellow = new Color(1f, 0.8f, 0f);
     private Color mygreen = new Color(0.45f, 0.8f, 0.3f);
     private Color myblue = new Color(0.1f, 0.65f, 0.87f);
     private Color myred = new Color(0.9f, 0.4f, 0.1f);
 
     public int flagColor;

     void Start()
     {
         renderer = transform.Find("flagSprite").GetComponent<Renderer>();
         animator = transform.Find("flagSprite").GetComponent<Animator>();
         particle = transform.Find("burst").GetComponent<ParticleSystem>();
 
         SetColor();
     }
 
     void OnEnable()
     {
         SetColor();
     }
 
     void OnDisable()
     {
         renderer.enabled = true;
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
         {
             renderer.enabled = false;
             particle.Play();
         }
     }
 
     void SetColor()
     {
         flagColor = Random.Range(0, maxcolor);
         animator.SetInteger("flagColor", flagColor);
         
         switch(flagColor)
         {
             case 0: //yellow
                 particle.startColor = myyellow;
                 break;
             case 1: //green
                 particle.startColor = mygreen;
                 break;
             case 2: //blue
                 particle.startColor = myblue;
                 break;
             case 3: //red
                 particle.startColor = myred;
                 break;
         }
     }
 }

Everything works as intended. There's no gameplay issue or noticeable bug, but I'm getting a NullReferenceException the first time renderer, particle, or animator is used. I can get rid of this error by replacing

 animator.SetInteger("flagColor", flagColor);


with

 transform.Find("flagSprite").GetComponent<Animator>().SetInteger("flagColor", flagColor);


but I shouldn't have to do that EVERY TIME, right? That's the point of storing the component in a variable.

EDIT: I get the NullReferenceException with the variables renderer, animator, and particle unless I replace all uses with with the full GetComponent.

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

Answer by Baalzamon · Feb 19, 2015 at 02:03 PM

Hi,

I had a similiar problem with assigning a component in the Start() method, but it is a component of the same GameObject.

Anyhow.

game = GetComponent()

would result in the error you described, whereas

game = GameObject.Find("Game").GetComponent();

throws no NullPointerReference Exception. I'm not sure if that's helpful for you. :)

I think it's a little odd, that you have to use the static Find() method to retrieve the gameobject, which should be stored in the gameObjectvariable anyways. Using game = gameObject.GetComponent() throws the same error though.

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

19 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

Related Questions

NullReferenceException-Error when trying to GetComponent 3 Answers

Why is this null? Finding a script on an object 3 Answers

NullReferenceException. GetComponent dose not work properly 2 Answers

NullRef on my parent script 1 Answer

GetComponent help 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