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 Exagerate · Feb 18, 2013 at 06:20 PM · nullreferenceexceptionitween

Object reference not set to an instance of an object

Hey there,

I have two C# scripts; one is a throttle lever (throttlelever.cs) that when clicked, should be triggering an iTween animation; the object to activate being given (caraccelerate.cs).

My error is

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

throttlehover.OnMouseDown () (at Assets/throttlehover.cs:17) UnityEngine.SendMouseEvents:DoSendMouseEvents() I'm Guessing they can't find one another, but I hope someone will be able to enlighten me on why that is. My throttle script: using UnityEngine; using System.Collections;
public class throttlehover : MonoBehaviour {    public caraccelerate accelerate;    void OnMouseEnter ()    {        renderer.material.color = Color.red;        }    void OnMouseExit ()    {        renderer.material.color = Color.white;        }    void OnMouseDown ()    {        accelerate.OnAccelerate();    }     } My caraccelerate vehicle script: using UnityEngine; using System.Collections;
public class caraccelerate : MonoBehaviour {
public void OnAccelerate ()    {        iTween.MoveTo(gameObject, iTween.Hash("path", iTweenPath.GetPath("centralline"), "time", 50, "easetype", iTween.EaseType.easeInOutSine));    }
} Kind regards,
Comment
Add comment · Show 5
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 ATMEthan · Feb 18, 2013 at 06:58 PM 0
Share

It sounds like you didn't assign anything to your accelerate variable. In the inspector since your accelerate variable is public you can just drag and drop the caraccelerate gameobject into that variable.

Or in your start method in the throttle script you can put something like

         accelerate = GameObject.FindObjectOfType(typeof(caraccelerate));

which will find a reference to a game object with a caraccelerate component on it and assign it to accelerate. (warning if there are more than one caraccelerate gameobjects it may not assign the correct one to your object)

So, basically your problem is your accelerate variable is empty/null and you need to assign it a caraccelerate reference.

avatar image dannyskim · Feb 18, 2013 at 07:00 PM 1
Share

I believe this needs to be added to AT$$anonymous$$Ethan's line of code:

 accelerate = GameObject.FindObjectOfType(typeof(caraccelerate)) as caraccelerate;
avatar image ATMEthan · Feb 18, 2013 at 07:02 PM 0
Share

Yes yes, use dannyskim's code snippet I forgot to add the "as caraccelrate" part. $$anonymous$$ine will generate an error complaining about casting a gameobject component to a caraccelerate component

avatar image Starkid225 · Feb 18, 2013 at 07:08 PM 0
Share

@dannyskim and @AT$$anonymous$$Ethan: These should really be answers and not comments. Its hard to mark an answer as the correct answer when it is a comment.

avatar image ATMEthan · Feb 18, 2013 at 07:19 PM 0
Share

You're right Starkid225, sorry about this inconvenience. I made my comment an answer now.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ATMEthan · Feb 18, 2013 at 07:21 PM

It sounds like you didn't assign anything to your accelerate variable. In the inspector since your accelerate variable is public you can just drag and drop the caraccelerate gameobject into that variable.

Or in your start method in the throttle script you can put something like

 accelerate = GameObject.FindObjectOfType(typeof(caraccelerate)) as caraccelerate;

which will find a reference to a game object with a caraccelerate component on it and assign it to accelerate. (warning if there are more than one caraccelerate gameobjects it may not assign the correct one to your object)

So, basically your problem is your accelerate variable is empty/null and you need to assign it a caraccelerate reference.

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 Exagerate · Feb 18, 2013 at 07:22 PM

Thanks guys! i was able to actually drag the caraccelerate (vehicle) from the Hierachy onto the Throttle script in the inspector and it did pretty much the same thing.

Kind regards,

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 ATMEthan · Feb 18, 2013 at 10:34 PM 0
Share

Hey, my answer my have not been posted before but they are there now. So, accept the answers as right so other users will know this helped/worked. :)

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

12 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

Related Questions

iTween NullReferenceException error 1 Answer

Using iTween with a prefab tree. 0 Answers

iTween call producing NullReferenceException 3 Answers

iTween - Null Reference 0 Answers

iTween nullref error. Hashtable syntax problem? Not sure. 3 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