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
1
Question by zcolek · Dec 25, 2012 at 05:16 PM · objectclassreferencenull

Null reference..

I know this quastion is being answered lot of times before, I searched and read lot topics, but none done work for me...

Here is the problem...

I have one class in a separete file that doesen't inheritate from Mono, just store all I need to use in Mono..

Now I have my mono class that is attached to Empty Game object.

This is the simple test, I need to work only OnGUI, everything is going to be 2D.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class test : MonoBehaviour  {

      proba t = new proba(10,10,100,100); //proba constructor and used for myRect

     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         if(t.myRect.Contains(Event.current.mousePosition)){
             if(Event.current.type == EventType.MouseUp){
                 t.myRect.x = 0;
             }
         }
     }
     
     void OnGUI(){
         GUI.Box(t.myRect,"");    
     }
 }


I have that little box on screen with parameters i passed to proba... but the problem is I am getting null reference when I want to click on that box, it doesn't know what is t.myRect.x... Everything in proba class is public, and here it is...

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class proba{
 
     public float x;
     public float y;
     public float w;
     public float h;
     public Rect myRect;
     public proba(float X,float Y,float W, float H){
         x = X;
         y = Y;
         w = W;
         h = H;
         myRect = new Rect(x,y,w,h);
     }
 }








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

And it is pointing to line 13 in test witch is begining of if statement where I have same check for Event...

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
2
Best Answer

Answer by Bunny83 · Dec 25, 2012 at 08:48 PM

It's quite simple: the Event class will only contain a valid event inside OnGUI. You use it in Update. In Update Event.current will always be null.

So just move your code in your OnGUI function. It's quite funny since you said in your question that you only use OnGUI but you actually use Update.

Comment
Add comment · Show 2 · 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 zcolek · Dec 25, 2012 at 09:31 PM 0
Share

That was enough... simple... Thx a lot, didn't knew that, now I can continue with my work...

avatar image Bunny83 · Dec 25, 2012 at 09:43 PM 0
Share

The documentation might help :)

avatar image
0

Answer by MarkFinn · Dec 25, 2012 at 07:11 PM

There is no event occurring in most frames so the Event.current is almost always null (and so has no mouse position)

Perhaps adding the check

 if(Event.current!=null && t.myRect.Contains(Event.current.mousePosition)){

will work?

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 zcolek · Dec 25, 2012 at 07:40 PM 0
Share

I will try that, but when I make a class only in one file and do the same code above I don't have any errors with Event.current.mousePostion? I don't want to have a huge mess of classes functions loops in one file... need to organize that.. I will post if your answer solved the problem. thx for reply

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

11 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

Related Questions

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

Object reference not set to an instance of an object 0 Answers

how to reference a healthbar from another gameobject 3 Answers

C# Null Object Reference - Driving me mad! 1 Answer

Object Pool object reference is null? 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