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 Oobi23 · May 26, 2013 at 01:32 AM · nullreferenceexceptionobject-reference-error

Null Reference - Help

Hi there, I am getting a weird "null reference object reference not set an instance of an object" error and am unable to stop the timer on collision. This is the code with the error and it's code is linked to another.

 function Start () {
 
 }
 
 function Update () {
 
 }
 
 
 function OnCollisionEnter(o:Collision){
     if(o.gameObject.name=="LOL"){
         var charScript:MovewithRaycast=GameObject.Find("CharContainer").GetComponent("MovewithRaycast") as MovewithRaycast;
 
         if(charScript.isShowingMission2()){
                 var t:Timer;
         t=GameObject.Find("Timer1").GetComponent("Timer1") as Timer;
             var timeTaken:int=t.stopTimer();
             Debug.Log("time taken was "+timeTaken);
         }
         
     }
     
     
 }


The error seems to be coming from the "var timeTaken:int=t.stopTimer()" bit. and Here is the code it's linked up to it

 private var doTimer:boolean;
 private var startTime:float;
 private var elapsedTime:float; 
 function Start () {
 doTimer=false;
 }
 
 function Update () {
     if(doTimer){
         elapsedTime=Time.time-startTime;
         GetComponent(GUIText).text = (elapsedTime).ToString("f2");
     
     }
 }
 
 
 function startTimer(){
     startTime=Time.time;
     doTimer=true;
 }
 
 function stopTimer(){
     
     doTimer=false;
     return parseInt(elapsedTime);
 }

any help is greatly appreciated.

Comment
Add comment · 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 FL · May 26, 2013 at 02:02 AM 0
Share

First, use $$anonymous$$athf.FloorToInt(elapsedTime) ins$$anonymous$$d of parseInt(elapsedTime). Check the elapsedTime value and if the Update() of your second script is called, I suggest you to call Update() at stopTimer() start.

avatar image Oobi23 · May 26, 2013 at 02:44 AM 0
Share

It doesn't work.. It still gives the null.. and I already have a replica of the same script working on another object, with $$anonymous$$or changes to not confuse unity.. Why is it working?.. This is the coding for the working one..

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 
 function OnCollisionEnter(o:Collision){
     if(o.gameObject.name=="EndBox"){
         var charScript:$$anonymous$$ovewithRaycast=GameObject.Find("CharContainer").GetComponent("$$anonymous$$ovewithRaycast") as $$anonymous$$ovewithRaycast;
 
         if(charScript.isShowing$$anonymous$$ission()){
                 var t:Timer;
         t=GameObject.Find("Timer").GetComponent("Timer") as Timer;
             var timeTaken:int=t.stopTimer();
             Debug.Log("time taken was "+timeTaken);
         }
         
     }
     
     
 }

and the script linked to it

 private var doTimer:boolean;
 private var startTime:float;
 private var elapsedTime:float; 
 function Start () {
 doTimer=false;
 }
 
 function Update () {
     if(doTimer){
         elapsedTime=Time.time-startTime;
         GetComponent(GUIText).text = (elapsedTime).ToString("f2");
     
     }
 }
 
 
 function startTimer(){
     startTime=Time.time;
     doTimer=true;
 }
 
 function stopTimer(){
     
     doTimer=false;
     return parseInt(elapsedTime);
 }
avatar image robertbu · May 26, 2013 at 03:41 AM 1
Share

Insert a debug state between line 16 and 17 to check if 't' is null. If it is, check to make absolutely you have a game object named 'Timer1' there is a component named 'Timer1' attached to that game object. Note the string must match exactly. If you find both are correct, then do a search (type in the search field at the top of the Hierarchy) for 'Timer1' to make sure you don't have two game objects named 'Timer1'...one with a 'Timer1' component, and one without.

avatar image Oobi23 · May 26, 2013 at 05:24 AM 0
Share

There aren't any other elements named Timer 1 except for the GUIText that has the script Timer1.js on it.. I have really been bashing my head with this problem for hours now..I wonder why the replica code of it works flawlessly though?

avatar image robertbu · May 26, 2013 at 05:31 AM 1
Share

Note in the preceding comment you called it 'Timer 1' with a space. But in the code above code you called it 'Timer1' without a space. I don't know if this is just a mistake in the comment or if this is the problem

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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

How to Connect Difficult Object using UnityEngine.Network 1 Answer

Script finds everything but still i get "nullreference, object not set to an instance of an object"??? 1 Answer

NullReferenceException: Object reference not set to an instance of an object Admanager.ShowFullScreenAd () 0 Answers

Bug in Object reference ? 0 Answers

Object reference not set to an instance of an object... 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