Getting a string from one script in an object to another script in a different object

Hi,

I need to get a variable from one script attached to a gameobject to another script attached to a different gameobject. I tried calling OtherScript.FunctionName(myString); but as usual I got: An instance of type ‘OtherScript’ is required to access non static member ‘FunctionName’.

What’s the most effective way to get around this?

Thanks!

If you have a reference to the object:

//set this in the editor
public ScriptA scriptA;

public string message = "Hello, my dear.";

void Start(){

[indent]
scriptA.SetMessage(message);
[/indent]

}

void SetMessage(string message){
[indent]
this.message = message;
Debug.Log(message + "!");
[/indent]
}

If you don’t - you need a way to identify it.

Whether this is by:

ScriptA script = GameObject.FindObjectOfType(typeof(ScriptA)) as ScriptA;
script.SetMessage(message);

Or some other mechanism.

You can also use the SendMessage() function, but I honestly have never used it so hopefully somebody else will jump in to point you in the right direction on that one.

Good luck!

(Note: these are code fragments. They’ll need to be in classes or such to work.)

-JFFM

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

–Eric