- Home /
Change gameobject sprite with button
Hi. Iam working on system, where button can change sprite render object. So i maked 2 scripts, first-
#pragma strict
public var sprajt: Sprite;
function Update() {
GetComponent(SpriteRenderer).sprite = sprajt;
}
This script is in gameobject. Second-
#pragma strict
public var sprajtTWO : Sprite;
function change (){
GetComponent(objectSCRIPT).sprajt = sprajtTWO;
}
that is in button fucntion OnClick... i want when someone pressbutton sprite render will change... so there is problem, error "Object reference not set to an instance of an object"... any solution / ideas how to make sprite render changer?
Answer by hbalint1 · May 03, 2015 at 10:59 PM
GetComponent searches on the gameobject the script it is mentioned on. so your script on the button has the GetComponent looks for the button to find onjectScript. that is not what you want. you should first find the gameobject who the first script is on, with GameObject.Find and after call GetComponent on it.
GameObject.Find("yourObjectsNameWithTheFirstScriptOn").GetComponent.<objectScript>().sprajt = sprajtTwo;
The code is untested.
Your answer
Follow this Question
Related Questions
[UNSOLVED] Object reference not set to an instance of an object 0 Answers
Help with changing spririterenderer's sprite 2 Answers
An instance of type 'TurretControl' is required to access non static member 'Shoot'. 1 Answer
Dialogue Code 0 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers