- Home /
The question is answered, right answer was accepted
Help a beginner with a small doubt ?
I just started learning unity and now I just started learning UnityScript API's. In this below script I am able to do everything I am doing for the object and its working properly but in below the code I am trying to access function from another script and it shows me an error message.
"NullReferenceException: Object reference not set to an instance of an object CubeBehaviour_01.Update () (at Assets/Scripts/CubeBehaviour_01.js:22)"
Here is the code
CubeBehavious.JS
var CubeSpeed = 1;
var accessVariable : GameObject;
function Update () {
this.transform.Translate(0,0,Time.deltaTime*CubeSpeed,Space.Self);
//Rotate the Object -> Cube.
this.transform.Rotate(0,0,1);
accessVariable.transform.Translate(0,0,Time.deltaTime*CubeSpeed,Space.Self);
var accessScript = GetComponent(SecondScript);
accessScript.newFunction("Hello");
}
SecondScript.js
function Update () {
}
function newFunction (s : String)
{
var newSpeed = 10;
print("Hello World " + newSpeed);
}
this newFunction is the function I am accessing.
Sorry if this is a very basic question. I am just a beginner . Thank you for your time.
Answer by Arcadewiz · Jul 30, 2014 at 07:26 AM
Is your script accessing another script that is present on the same game object in your Hierarchy view or from a different game object.
If the "SecondScript" is attached to a different game object then you are required to reference that and add getcomponent to that referenced gameobject after finding it.
so bad I made such a simple mistake. I forgot to attach the secondScript to the object which has the firstscript where I am working in.
Thanks a lot. It all worked :)
Follow this Question
Related Questions
Help with OnCollision 2 Answers
How to make a simple ladder? 3 Answers
Health Script Help...? 1 Answer
How to pass function as parameter to function? 1 Answer
How to delete new'd arrays? 2 Answers