- Home /
Question by
AhmedGalal1987 · Oct 24, 2016 at 02:15 PM ·
uigameobjectbutton
Can't to get Button script from gameobject in unity
I have some button in my game and i want when host player answers and choose one button to get the button script of this game object button to change its color.
But i get some error like that :
NullReferenceException: Object reference not set to an instance of an object
This is my code :
Button playerOneAnswerBtn = GameObject.FindWithTag (playerOneAnswer).GetComponent<Button> ();
ColorBlock cbPlayerOneAnswer = playerOneAnswerBtn.colors;
cbPlayerOneAnswer.normalColor = Color.blue;
cbPlayerOneAnswer.highlightedColor = Color.blue;
playerOneAnswerBtn.colors = cbPlayerOneAnswer;
Comment
@AhmedGalal1987 There no object with the specified tag.
GameObject o=GameObject.FindWithTag (playerOneAnswer);
if(o){
Button playerOneAnswerBtn = o.GetComponent<Button> ();
if(playerOneAnswerBtn){
ColorBlock cbPlayerOneAnswer = playerOneAnswerBtn.colors;
cbPlayerOneAnswer.normalColor = Color.blue;
cbPlayerOneAnswer.highlightedColor = Color.blue;
playerOneAnswerBtn.colors = cbPlayerOneAnswer;
} else {
Debug.Log("No Button Component found.");
}
}else{
Debug.Log("No object found with Tag: "+playerOneAnswer);
}
Your answer
Follow this Question
Related Questions
Detect when a UI button is not pressed and change GO transform rotation. 2 Answers
Gathering GameObjects and then creating a button for each GameObject?? 2 Answers
Buttons still visible after Destroy() but say "deleted gameobject" in hierarchy 1 Answer
Unity ads , can't we show video by clicking a button? 1 Answer