I have a single GameObject called ‘player’. I have two scripts attached to this single gameObject, a c# script called “animaConnect” and a JS script called “PlayerController”.
I have the C# script in the ‘Standard Assets’ folder.
I have the JS script outside the ‘Standard Assets’ folder.
‘AnimaConnect’ (c#) has a function called ‘getTriggers’:
private void getTriggers()
{
print("HELLO");
}
In my JS update function:
public function Update()
{
otherscript = GetComponent(AnimaConnect);
triggers = otherscript.getTriggers();
}
I get a “Null Reference Exception”. So I think I have my syntax wrong somewhere? Also, what is the syntax when accessing a JS function from C#? I have found some example and got it to work but only when using the ‘Find()’ method, this isn’t needed when both scripts are attached to the same GameObject, right?
Whoops, I just noticed that the C# function is private.
Changing to public now prints - (HELLO) but it still generates the error. Any help? Syntax for C# calling JS function on same component?
Ok, I thought it was working but it’s not.
Lets start again:
C#:
public void getTriggers()
{
print ("HELLO");
}
JS:
public function Update()
{
animaConnect = GetComponent(AnimaConnect);
animaConnect.getTriggers();
}
Doesn’t print anything and generates the NullReferenceException. Both scripts are attached to the same GameObject.
Read Unity - Scripting API:
(second to last paragraph is the important one)
In summary: scripts in the same compilation group cannot talk to each other if they are written in different languages.
Thankyou, I read that but didn’t take enough notice.
The C# script was correctly in the ‘Standard Assets’ folder.
My JS was outside this in my own folder, outside ‘Standard Assets’ named ‘MyScripts’. It appears that the JS script needs to be outside this too, at the top level of the folder hierarchy.