Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Klousianic · Aug 02, 2016 at 01:47 PM · c#nullreferenceexceptiongetcomponentontriggerenterspriterenderer

GetComponent null, but it's attached to gameobject

Hey everybody, so I wrote a script, which creates two objects. In one of these object scripts, it want to get the objects SpriteRenderer component and it's attached to! But everytime I run the game it says NullReferenceException. The line with the error is a comparison between the spriterenderer color of the objects, so I check first if one of them is null.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.SceneManagement;
 
 public class LockDoor : MonoBehaviour {
 
     public List<Color> colGen = new List<Color>();
     public int colNow;
     public int i;
     public Transform doorsGO;
     public Animation lockAnim;
     public Controls controls;
     public ScoreManagement scoreScript;
 
     private new SpriteRenderer renderer;
 
     void Awake()
     {
 
         // Find GameObejcts
         doorsGO = GameObject.FindWithTag("doors").transform;
         scoreScript = GameObject.Find("Score").GetComponent<ScoreManagement>();
         //doorsGameObject = GameObject.Find("doors");
         controls = doorsGO.GetComponent<Controls>();
 
 
     }
 
     // Use this for initialization
     void Start () {
 
         // GetComponents
 
         lockAnim = GetComponent<Animation>();
         lockAnim["LockTheDoor"].speed = 2.0f;
 
         // Liste mit Farben
 
         colGen.Add(Color.blue);
         colGen.Add(Color.green);
         colGen.Add(Color.yellow);
         colGen.Add(Color.red);
 
         // Change Color für Schlüssel
 
         renderer = GetComponent<SpriteRenderer>();
         colNow = Random.Range(0, colGen.Count);
         renderer.color = colGen[colNow];
 
         // Change Color für Türen
 
         foreach (Transform colChi in doorsGO)
         {
             SpriteRenderer col = colChi.GetComponent<SpriteRenderer>();
             i = Random.Range(0, colGen.Count);
             col.color = colGen[i];
             colGen.RemoveAt(i);
         }
 
     }
 
     IEnumerator WaitForAnimation () {
 
         lockAnim.Play();
         controls.enabled = false;
         scoreScript.enabled = true;
         print("You've locked the door!");
         yield return new WaitForSeconds(lockAnim.clip.length);
 
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
 
         SpriteRenderer othCol; 
         othCol = other.gameObject.GetComponent<SpriteRenderer>();
         if (renderer != null)
         {
             if (renderer.color == othCol.color) // here is the line with the error
             {
                 StartCoroutine(WaitForAnimation());
             }
             else
             {
                 print("Wrong door!");
             }
         }
         
     }
 
    
 }
 

Thanks and sorry for my bad english!

Comment
Add comment · Show 2
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 Landern · Aug 02, 2016 at 02:26 PM 0
Share

The gameObject referenced to get the SpriteRenderer component doesn't have one directly attached to the gameObject. Is the collider pointing to a top level object in the hierarchy(look at the panel for the object hierarchy). You may want to try using GetComponentInChildren since it will transverse the object graph looking for SpriteRenderer types, this is more expensive of course, but it seems like you're assu$$anonymous$$g the SpriteRenderer exists at a particular position in the gameobject hierarchy.

avatar image Klousianic Landern · Aug 02, 2016 at 10:27 PM 0
Share

Thanks for the comment, but I don't know what you really mean. The object that colliding with this object, is a child, but I thought that this isn't important, because the Collider just look for the other object(in OnTriggerEnter), and not whether it's a child of an other object. But I tried it out, and the error is still there :/

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by IgorAherne · Mar 12, 2017 at 06:54 PM

It's been ages and not really related to a question, but the post is a perfect opportunity to explain an issue I was having.

I was instantiating a prefab from within a Coroutine, and calling GetComponent immediately afterwards. Turns out, I had to yield return null before attempting to getComponent inside this corutine, to give unity a chance to set-up the components.

That's true because when breakpointing, the Start() method in the instantiated prefab wasn't hit until the next frame.

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

215 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 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 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 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 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 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 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 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 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 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

Tanks Tutorial: Accessing TankHealth 1 Answer

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

Null Reference Exception on Sprite Renderer 0 Answers

Start Finish Timer 0 Answers

Having troubles changing my players speed on collision 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