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 Rimbodie · Jan 02, 2014 at 11:40 PM · nullreferenceexception

NullReferenceException?

I've got a very simple script from a tutorial, but when I try to run it, it gives the NullReferenceException error in the console everytime I press one of the arrow keys.

Anyone that knows what might be wrong with my code?

Learningscript.cs: using UnityEngine; using System.Collections;

 public class LearningScript : MonoBehaviour 
 {
     GameObject capsuleGO;
     Spinner cubeComp;
     
     void start() 
     {
         capsuleGO = GameObject.Find ("Capsule");
         Debug.Log (capsuleGO);
         cubeComp = GameObject.Find ("Cube").GetComponent<Spinner>();
         Debug.Log(cubeComp);
     }
     
     void Update() 
     {
         if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             capsuleGO.GetComponent<Spinner> ().SpinLeft();
         }
         
         if(Input.GetKey (KeyCode.RightArrow))
         {
             capsuleGO.GetComponent<Spinner>().SpinRight();
         }
         
         if(Input.GetKey (KeyCode.UpArrow))
         {
             cubeComp.SpinLeft();
         }
         
         if(Input.GetKey (KeyCode.DownArrow))
         {
             cubeComp.SpinRight();
         }
     }
 }

spinner.cs:

 using UnityEngine;
 using System.Collections;
 
 public class Spinner : MonoBehaviour {
 
 public void SpinLeft()
     {
         transform.Rotate (0, 0, 60 * Time.deltaTime);
     }
 
     public void SpinRight()
     {
         transform.Rotate(0, 0, -60 * Time.deltaTime);
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by rutter · Jan 02, 2014 at 11:45 PM

I'll assume you understand what a NullReferenceException is. It's fine if you don't. Google might help, or just ask in a comment here.

So, you have two references: capsuleGO and cubeComp. Both are populated by a combination of GameObject.Find() and GetComponent().

  • If there is no GameObject named "Capsule", capsuleGO will be null

  • If there is no GameObject named "Cube", you'll get a null reference exception at start

  • If "Cube" exists but has no Spinner component attached, cubeComp will be null

Finally, C# is case-sensitive: start and Start are not the same name. Unity is looking for a function named Start.

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 Rimbodie · Jan 03, 2014 at 11:51 AM 0
Share

Thanks! I triple checked the code the tutorial had written out, can't believe I missed that haha

avatar image
0

Answer by mohamedk2014 · Jun 11, 2019 at 04:06 PM

i have this problem this is: use capital letter in "cube" and "capsule" because the name of the game object in unity for those items begin with capital letter

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

20 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

Related Questions

troubleshooting NullReferenceException 2 Answers

NullReferenceException 2 Answers

Material[] Object reference not set when instantiating 2 Answers

NullReference Exception when adding a class 2 Answers

array initialisation c# 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