Access functions across scripts 2

I have a script to setup a cow character called cowscript I want to call many functions in the mainscript attached to object called main.

So cowscript is attched to cow
mainscript is attached to main

The function in the mainscript is called getData()

So how would I access the getData() function from within cowscript? I want to access many functions in the main script not just that one so is there a solution that would give me access to all the functions in mainscript?

Dan

What you do is first make a top variable of that script type in the Cow Script.

"var MAIN : MainScript; //(replace “MainScript” with the name of the script in the object Main)

Then create a new Tag for the Main object. In my example I give it the unique Tag “MASTER”

then when the scripts starts, set MAIN equal to the MainScript on the object with the tag MASTER.

function Start()
{
MAIN = GameObject.FindWithTag("MASTER").GetComponent("MainScript")
}

then in cow script you’ll be able to access functions from the MainScript via methods using the MAIN variable.

MAIN.GetData();

MAIN.AddPoints(5);

Doesn’t seem to work

Am I missing a step?

Here’s what I have the names are the actual names now because the first names I gave were example names and may be causing some confusion.

Cow with script cowsetup.js
CSB which is the “main” object the script attached to it is called world.js. I want to run functions in world.js from the cowsetup.js script.

Now in cowsetup.js I put
var MAIN:world;// at the top

in start I put
MAIN = GameObject.Find(“CSB”).GetComponent(“World”);

Then later I try to use
Main.getData() which is my own function in the world.js script.

But I get the error that MAIN.getData()
NullReferenceException: object reference not set to an instance of an object

Am I missing a step?

Dan

GameObject.Find(“CSB”).GetComponent(“World”);

make sure that the component you are looking for is spelled exactly as you have it in the assets window.

I’m pretty sure GetComponent is cap and space sensitive.

So try:
GameObject.Find(“CSB”).GetComponent(“world”);

They are right in the code I just wrote it incorrectly.
Could it have something to do with cowscript being assigned dynamically
to cow in csb?
Or the fact that cow is generated from csb dynamically?

no cause if it is done correctly when the instance of the script is created it finds CSB and assigns the var with the unique id of the script.

I know it works cause I do it all the time.

The problem is it is either not finding CSB and returning null, or it is not finding the script on the gameobject and returning null.

try doing it in two steps rather than 1 and see which fails.

var CSBob : GameObject;
var MAIN : world;

function Start()
{
CSBob = GameObject.Find("CSB");
MAIN = CSBob.GetComponent("world");
}

then run the program, and after the cow is Generated with the script; pause and examine the cow in the editor. You’ll be able to see if CSBob/MAIN were set to something or if they are null.

if both are null then the problem lies with finding the game object, if only MAIN is null then the problem is with the script not finding "world"component on the CSB object.

p.s here is an example of me accessing a function from another script on an object that is istanced.

other.transform.GetComponent("Bumper").RtnSize()

I found the problem. I was calling a function in the main script before the attachment or process of assinging the script to the variable occured.

it was still Null, shortly afterwards it became “world”

Thanks for helping me get this working.

Dan

Awesome! :smile: