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 Abhi94 · Jun 08, 2015 at 11:02 AM · buttononclickscripterror

Can anyone tell me why script is not working?

I am trying to attach this script to my button "onclick" function , so that when i click it and if its color is same as displaycolor "hello" is displayed . But i am getting error message saying- "Object reference not set to an instance of an object" . SO please kindly help.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class OnClick : MonoBehaviour {
     
 
 
 
 public    void SayHello(string hello
                          ) {
 
     
         if (GetComponent<Image>().color == GameObject.FindGameObjectWithTag("DisplayButton").GetComponent<Image>().color)
         {
 
             Debug.Log("Hello");
         }
         
         }
         
         }
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 Bonfire-Boy · Jun 08, 2015 at 11:24 AM 1
Share

Chances are that either you don't have a gameobject with the "DisplayButton" tag, or you have one that doesn't have an Image component.

Try breaking line 14 down into steps and adding logging so you can see what the issue is, something like

 GameObject g = GameObject.FindGameObjectWithTag("DisplayButton");
 if (g == null)
 {
   Debug.LogError("No tagged DisplayButton");
 }
 else
 {
   Image i = g.GetComponent<Image>().color)
   if (i == null)
   {
     Debug.LogError("DisplayButton "+g.name+" doesn't have an Image component");
   }
   else
   {
     GetComponent<Image>().color = i.color;
   }
 }
 
 
          

avatar image Abhi94 · Jun 08, 2015 at 03:40 PM 0
Share

well ty mate , so is this what you want me to put?

using UnityEngine; using System.Collections; using UnityEngine.UI;

  public class OnClick : $$anonymous$$onoBehaviour {
      
  
  
  
  public    void SayHello(string hello
                           ) {
  
      
        GameObject g = GameObject.FindGameObjectWithTag("DisplayButton");
  if (g == null)
  {
    Debug.LogError("No tagged DisplayButton");
  }
  else
  {
    Image i = g.GetComponent<Image>().color)
    if (i == null)
    {
      Debug.LogError("DisplayButton "+g.name+" doesn't have an Image component");
    }
    else
    {
      GetComponent<Image>().color = i.color;
    }
  }
          }
          
          }

avatar image Bonfire-Boy · Jun 08, 2015 at 04:01 PM 0
Share

Well, yes, that is what you get when you replace line 14 with what I put. I didn't test the code. Have you tried it?

On a second look I made a mistake in that I'm setting the color ins$$anonymous$$d of testing it. Sorry about that. The last substantive line of my code (line 24 of your last snippet) should be changed to

 if (GetComponent<Image>().color == i.color)
 { 
     Debug.Log("Hello");
 }

in order for it to do what your original code did.

The idea was just to show you a way of working out what it is that's missing.

Logging stuff like this is often helpful when debugging in unity, especially when a line of code like that can fail in several places. It's often impossible for other people to tell what's wrong just by looking at the code, so you need techniques like this to enable you to track the issue down yourself.

avatar image Abhi94 · Jun 08, 2015 at 04:51 PM 0
Share

i am getting error that cant convert "UnityEngine.color to UnityEngine.ui.image" at line 20

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class OnClick : $$anonymous$$onoBehaviour {
     
     
     
     
     public    void SayHello() {
         
         
         GameObject g = GameObject.FindGameObjectWithTag("DisplayButton");
         if (g == null)
         {
             Debug.LogError("No tagged DisplayButton");
         }
         else
         {
             **Image i = g.GetComponent<Image>().color;**
                 if (i == null)
             {
                 Debug.LogError("DisplayButton "+g.name+" doesn't have an Image component");
             }
             else
             {
                 if (GetComponent<Image>().color == i.color)
                 { 
                     Debug.Log("Hello");
                 }
             }
         }
     }
 }    
avatar image Bonfire-Boy · Jun 08, 2015 at 05:23 PM 0
Share

That error message is telling you that you need an Image but are providing a Color.

GetComponent< Image >() gives you an Image, the .color bit gets you a Color. You want an Image there, so remove the .color bit.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bioinformatizer · Jun 08, 2015 at 04:27 PM

You could also eliminate the 'string hello' from the method call. Unless you have the button pass a string into the method when you call it. Another piece of information that would be helpful to figure this out would be the line number in monodevelope that your "Object reference not set to an instance of an object" comes from. Best of luck!

Comment
Add comment · Show 16 · 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 Abhi94 · Jun 08, 2015 at 05:03 PM 0
Share

Sure i would try ti explain more if that could help me. I am getting the error "Object reference not set to an instance of an object" in line 13. Hope it helps because i really need it

 `
  using UnityEngine;
  using System.Collections;
  using UnityEngine.UI;
  
  public class OnClick : $$anonymous$$onoBehaviour {
      
  
  
  
  public    void SayHello(string hello
                           ) {
  
      
          **if (GetComponent<Image>().color == GameObject.FindGameObjectWithTag("DisplayButton").GetComponent<Image>().color)**
          {
  
              Debug.Log("Hello");
          }
          
          }
          
          }`


avatar image Bioinformatizer · Jun 08, 2015 at 05:13 PM 0
Share

Yup, you have to drop the 'string hello'. For your other error, cannot convert color to image, just change line 20 from Image to Color. Then drop the 'color' from i.color on line 27.

avatar image Abhi94 · Jun 09, 2015 at 07:34 AM 0
Share

I am still getting message that " Object reference not set to an instance of an object" at line 27

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class OnClick : $$anonymous$$onoBehaviour {
     
     
     
     
     public    void SayHello() {
         
         
         GameObject g = GameObject.FindGameObjectWithTag("DisplayButton");
         if (g == null)
         {
             Debug.LogError("No tagged DisplayButton");
         }
         else
         {
             Color i = g.GetComponent<Image>().color;
                 if (i == null)
             {
                 Debug.LogError("DisplayButton "+g.name+" doesn't have an Image component");
             }
             else
             {
                 if (GetComponent<Image>().color == i)
                 { 
                     Debug.Log("Hello");
                 }
             }
         }
     }
 }
avatar image Bioinformatizer · Jun 09, 2015 at 12:27 PM 2
Share

That 'GetComponent().color' needs to be a method of a GameObject. Right now it looks like your script only relies on one GameObject, g. It then stores the color of g as the Color i. If you want to compare another color, you need add another GameObject or another Color.

The 'GetComponent().color' right now is trying to ask a question similar to 'what color is your car?' to someone with no car. Since they have no car to have a color on, they can't tell you the color.

As you mentioned earlier, this is attached to button already, correct? A method to get the button this script is attached to is by simply referencing gameObject (capitalization matters here). This should return the button you have attached the script to and then you can store that as a GameObject to use your 'GetComponent().color ' on. Best of luck!

avatar image Bonfire-Boy · Jun 09, 2015 at 01:47 PM 1
Share

"I am still getting message that " Object reference not set to an instance of an object" at line 27"

Excellent, so that means the "current" gameobject (the one the OnClick is attached to) does not have an Image component. Do you see how the logging helped you to work out which thing it was that wasn't set to anything?

So you either need to add an Image component to that object, or find some way of referencing the object whose Image component you're trying to set the colour of.

Show more comments
avatar image
0
Wiki

Answer by starikcetin · Jun 09, 2015 at 02:34 PM

Take a look at the code in this link: http://forum.unity3d.com/threads/check-if-a-gameobject-has-a-certain-script.88484/#post-571809

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

i want my star () function to work my button is clicked 0 Answers

[Solved] Button OnClick properties are missing after loading the scene 4 Answers

Passing through a GameObject/Function to a button's OnClick 1 Answer

List of Eventlisteners to add to a Button on demand? 0 Answers

Button doesn't work when returning to scene (android) 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