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 Brokenarrow · May 23, 2013 at 12:14 PM · c#getcomponentnullaccess

GetComponent keeps returning null

Hi all, this is my first post here, I'm currently learning Unity next to my CG work. All is going well so far, with the exception of the following problem, which really has me stumped.

Im trying to access a script called "Car1health" on the same object, called Car1. This script contains a variable, public float "carCurrenthealth", with a starting value of 10. I do this with the following code:

Car1health other = gameObject.GetComponent< Car1health >(); if(other == null) Debug.Log ("other is null"); else if(other.carCurrenthealth < 0){ Application.LoadLevel("Player2won");}

Yet the function keeps returning null. What am I doing wrong?

Any insights would be much appriciated:)

Update: After some more testing, the script actually does execute, (it loads the next scene when the variable reaches 0) but gives a nullreferenceexception at start, unless I add an exception for when the GetComponent retrieves null. Any insights about why this is happening?

Comment
Add comment · Show 3
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 loopyllama · May 23, 2013 at 08:35 PM 0
Share

Does Car1helath get added dynamically? If yes, add it earlier, like in Awake ins$$anonymous$$d of Start.

avatar image loopyllama · May 24, 2013 at 10:36 AM 0
Share

Car1health is fine. Some prints will help you out. You may have that first chunk of code on an object that does not have. Car1health or maybe Car1 doesn't get created right away. Before that first chunk of code type something like print ("attached to " + gameObject + " at time " + Time.time); and put the same thing in an Awake in your Car1health. That will show you what the problem is.

avatar image Brokenarrow · May 30, 2013 at 11:01 AM 0
Share

Thank you :) I figured it out with that trick! By putting a timer on the GetComponent I made sure Car1health existed before GetComponent was called.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by loopyllama · May 23, 2013 at 12:19 PM

if your script is called Car1 then your GetComponent should be getting Car1 rather than Car1health

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 Brokenarrow · May 23, 2013 at 03:17 PM 1
Share

The object is called Car1, the script Car1health. I can see why you misunderstood though,it was written in an odd way. I edited the question to be more clear.

avatar image
0

Answer by Brokenarrow · May 24, 2013 at 06:57 AM

The variable carCurrenthealth(in the "Car1health"script) is declared at monobehaviour, where the actual code which calculates damage taken is in Update. Declaring the variable in Awake doesnt change anything, and I assume I can't put the damage calculation in Awake, since I have to check for damage every frame.

However, after some more testing I found it actually does work, although it still gives me a nullreferenceexception at start. Afterwards it retrieves the value just fine. I think you are right about declaring the variable earlier, but I am unsure how.

I added the relevant part of the Car1health script below, if it gives any insights.

 using UnityEngine;
 using System.Collections;
 
 public class Car1health : MonoBehaviour
 {
 
     public float carCurrenthealth = 10;
     public float collisionDamage = 1;
     
 
 
     void DamageTaken(){
         carCurrenthealth -= collisionDamage;
     }
     
 
     
     void OnCollisionEnter(Collision collision){
         
         if(collision.gameObject.CompareTag("Car2"))
 
         DamageTaken();
         Debug.Log("Car 1 Health: " + carCurrenthealth);
 }
         
 
     }

I really appreciate the help and quick answers by the way:)

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 fnc12 · Apr 29, 2014 at 10:20 AM

Check in inspector if you forgot to add a component to your object

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

15 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

Related Questions

Can't get script to access other script/class (c#) 1 Answer

Have null errors 1 Answer

Change and Access another scripts Variables 1 Answer

Accessing a boolean from another script with GetComponent (USING C#!) 1 Answer

how to make one script change a variable in another scipt 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