I tried to call a c-script function (IO_control.cs, under stardard assets) from my Javascript. The error message in Console window is
NullReferenceException: Object reference not set to an instance of an object
in my Javascript file:
private io_cs : IO_control ;
function Awake () {
io_cs = this.GetComponent("IO_control");
}
function Update () {
io_cs.performActions();
}
I tried io_cs.performActions(); io_cs.IO_control.performActions();
Both have same error message, what is the right way to call the function here?
name of cs file: IO_control.cs
structure in the cs file:
using UnityEngine;
using System.Collections;
using System;
public class IO_control : MonoBehaviour {
public void performActions() {}
}
A NullReferenceException is in 99% of all cases a problem that GetComponent can’t find the script on the Gameobject. Are you sure that both scripts are attached to the same GameObject?
Btw. i guess your variable declaration in your UnityScript should be:
private var io_cs : IO_control;
Besides that the code is ok. You should use your first way, the second way doesn’t make any sense either …
io_cs.performActions();
The null reference exception happens because your io_cs variable doesn’t point to an instance of that script. In your case that means that GetComponent returned “null” because it can’t find the component.
Thank you for comments. I didn’t attach .cs to the same Gameobject.
Please don't use Answers for comments. On a question and answer site like this you can use the "add new comment" button below an answer to comment on it, also remember to "thumbs up" answers that helped you and mark one as correct. Doing things this way helps keep this site an easily searchable index of common pitfalls and problems so newcomers can quickly and easily learn from all of our mistakes.
.cs files are C#, not C.
– fherbst