- Home /
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");
}
}
}
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;
}
}
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;
}
}
}
}
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.
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");
}
}
}
}
}
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.
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!
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");
}
}
}`
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.
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");
}
}
}
}
}
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!
"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.
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
Your answer
Follow this Question
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