Change gameobject sprite with button

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.

#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?

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.