• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by joseaguirre · Sep 11, 2015 at 10:56 PM · 2dtriggercounternull reference exceptionscript reference

Why do I keep getting a null reference? Its not null!

Hello fellow programmers at unity answers, I have been trying to fix this stupid code for like 30 minutes with no results yet.

I have two pieces of UnityScript...... I keep getting a null reference error on line 7 of the first code.....

 var script:numberAttempsLogger;
 function OnTriggerEnter2D (other : Collider2D) {
 
    if(other.gameObject.tag == "Player"){
     script = GetComponent (numberAttempsLogger);  
     yield WaitForSeconds(1); 
     script.attempts +=1;
         
     }
 }
 

This code is so simple and idk why it keeps giving me this error

here is the script it is referencing below

 var attemptsCounter : GUIText;
 var attempts: int = 1;
 function Awake() {
     
     DontDestroyOnLoad(Transform.GameObject);
 
 }
 function Update() {
     attemptsCounter.text = "Attempts : "+ attempts;
 }
 

Am I making a simple syntax mistake?? because im pretty sure i set this up right....if not, can someone please send me the documentation that shows me how to fix it? I have read everything i can find on this

Comment

People who like this

0 Show 6
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 hulahoolgames · Sep 11, 2015 at 11:21 PM 0
Share

numberAttempsLogger script is attached to the same gameObject as the first script? or is it attached on the player gameObject?

avatar image joseaguirre hulahoolgames · Sep 11, 2015 at 11:24 PM 0
Share

the logger script is attached to an empty gameobject that will be called when the game first starts....so as the player progresses through the levels, the script will still keep count of the amount of deaths in the game

avatar image hulahoolgames joseaguirre · Sep 11, 2015 at 11:32 PM 0
Share

okay, GetComponent will search the components of the current gameObject, so script = GetComponent (numberAttempsLogger); will try and check for the script on the current gameObject the first script is attached to. Maybe you want to change this line to be script = emptyObj.GetComponent (numberAttempsLogger); where emptyObj is the empty gameobject reference your script is attached to. Hope I am not misunderstanding your setup.

Show more comments
Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

32 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

Related Questions

Problems with using OnTriggerStay2d 0 Answers

Detect the objects staying on top of the Particles 0 Answers

Does an object need to be a trigger to interact with another one 0 Answers

Object not being dragged 1 Answer

How do i extend a animation ? 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