Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by PaxStyle · Oct 30, 2015 at 09:07 PM · scripting problemobjectinstance

Object reference not set to an instance of an object [problem with Unity 5]

Hi to all.. I imported a project that i've made with unity 3 and there wasn't errors, when I import the project in Unity 5 this script give me this error: Object reference not set to an instance of an object.. Why in Unity 3 I don't have this problem? :( The line where ther's the error is ray = Camera.main.ScreenPointToRay(new Vector2(Input.mousePosition.x, Input.mousePosition.y)); Hope someone can help me. :)

Here the full script

  private var decrement : float;
     private var toggle : boolean;
     private var ray : Ray;
     private var hitInfo : RaycastHit;
     
     
     var Audio : AudioClip;
      
     function Start()
     {
         decrement = transform.localScale.y / 1.3f; //modificare questo valore per ridurre l'altezza.
     } 
      
     function OnMouseDown()
     {   
     
     if(!enabled) return;
     
         ray = Camera.main.ScreenPointToRay(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
     
         if(Physics.Raycast(ray, hitInfo))
         {
             if(hitInfo.transform.gameObject == gameObject)
             {
                 if(Input.GetMouseButton)
                 {
                     if(!toggle)
                     {
                         transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y - decrement, transform.localScale.z);
                         transform.position.y += decrement / 2;
                         toggle = true;
                         GetComponent.<AudioSource>().clip = Audio;
                         GetComponent.<AudioSource>().Play();
                     }
     
                     else
                     {
                         transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y + decrement, transform.localScale.z);
                         transform.position.y -= decrement / 2;
                         toggle = false;
                         GetComponent.<AudioSource>().clip = Audio;
                         GetComponent.<AudioSource>().Play();
                     }
                 }
             }
         }
     }
     

Comment
Add comment · Show 1
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 PaxStyle · Oct 30, 2015 at 09:21 PM 0
Share

Could someone help me? I don't know how to fix this problem

2 Replies

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

Answer by hexagonius · Oct 31, 2015 at 11:32 AM

Either your scene does not have a Camera, OR that camera is not tagged as "Main Camera". At least that's the only object in that code line that might be not found.

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 PaxStyle · Oct 31, 2015 at 11:35 AM 0
Share

Yess! Thank you !:)

avatar image
0

Answer by Tom01098 · Oct 31, 2015 at 11:24 AM

Object reference not set to an instance of an object generally means you haven't dragged and dropped a gameobject in the inspector.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Fix this issue 1 Answer

Cant find text objects after being renamed. 1 Answer

Dragging an object to variable: Problems in the editor! 2 Answers

“Object reference not set to an instance of an object 1 Answer

how do i rotate an object? 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