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 ofltunisia · Mar 16, 2016 at 02:12 AM · c#buttonnullreferenceexception

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

hello there , i making a Question and answer game in unity , but i get this problem whenever i try to check for the correct response , i have three scripts, the first one game_uGUI which basically set the questions and answers from the files , the second is call_win and the third is call_lose.

i have four buttons in the scene and everything is attached to its place scripts and everything

but one i test, it gives back : NullReferenceException: Object reference not set to an instance of an object game_uGUI.button_two_logic () (at Assets/mobile game menu kit/script/game_scene/game_uGUI.cs:414)

maybe my approach here is causing the problem as i am calling onMousDown() from both call_win& call_lose. this is game_uGUI script public void button_four_logic() { if (response_4.text.CompareTo(correct_rsp) == 0) { call_win.OnMouseDown(); } else call_lose.OnMouseDown(); }

     public void button_one_logic()
     {
         if (response_1.text.CompareTo(correct_rsp) == 0)
         {
             call_win.OnMouseDown();
         }
         else
             call_lose.OnMouseDown();
 
     }
     public void button_two_logic()
     {
         if (response_2.text.CompareTo(correct_rsp) == 0)
         
             call_win.OnMouseDown();
         
         else
             call_lose.OnMouseDown();
     }
 
     public void button_three_logic()
     {
         if (response_3.text.CompareTo(correct_rsp) == 0)
         {
             call_win.OnMouseDown();
         }
         else
             call_lose.OnMouseDown();
     }
 

this is call_win script

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class call_win : MonoBehaviour {
 
     public int star_score;
     game_uGUI my_game_uGUI;
     public int score;
     public Text pscore;
     void Start()
     {
         my_game_uGUI = GameObject.FindGameObjectWithTag("_gui_").GetComponent<game_uGUI>();
 
     }
 
     public    void OnMouseDown ()
     {
         if (!game_uGUI.in_pause)
             {
             my_game_uGUI.star_number = star_score;
             pscore.text = "100";
             my_game_uGUI.Victory();
             }
     }
 }
 

this is the call_lose script: using UnityEngine; using System.Collections;

 public class call_lose : MonoBehaviour {
 
     game_uGUI my_game_uGUI;
 
     void Start()
     {
         my_game_uGUI = GameObject.FindGameObjectWithTag("_gui_").GetComponent<game_uGUI>();
     }
 
     public    void OnMouseDown ()
     {
         if (!game_uGUI.in_pause)
             {
             my_game_uGUI.Defeat();
             }
     }
 }
 
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 ZombieMcFlounder · Mar 16, 2016 at 02:16 AM 0
Share

you might have to specify which button left click is if(Input.GetButtonDown("Fire1")) { right click is if(Input.GetButtonDown("Fire2")) {

1 Reply

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

Answer by toddisarockstar · Mar 16, 2016 at 02:40 AM

your error means that whatever gameobject variable your script is reffering to is empty. you are making your script try to check a gameobject variable that hasn't been filled yet.

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 ofltunisia · Mar 16, 2016 at 03:32 AM 0
Share

but here i am just calling the function from call_win.on$$anonymous$$ouseDown(). Should i set both call_win and call_lose script to an object ?

avatar image ofltunisia · Mar 16, 2016 at 03:56 AM 0
Share

well i attached both scripts to the buttons put i still have the same problem

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

119 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 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 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 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 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 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

There's an issue with this script I can't find 0 Answers

Next and Back Button? 0 Answers

How to get touch's positon when I hold the button?? 2 Answers

got trouble:object reference not set to a instance of an object 1 Answer

Creating dynamically moving buttons/GUI objects. 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