call c-script from Javascript

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 () {

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 …


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.

Frist, your “private io_cs : IO_control;” should become public, so you can actually assign something to it: “public io_cs : IO_control;”.

Then, be sure to drag your

(gameObject with the IO_control script)

onto the io_cs slot on your

(gameObject with the Javascript file)

in the inspector, like you would do with a Transform or something else.

Thank you for comments. I didn’t attach .cs to the same Gameobject.