- Home /
Cant call function in js another script
Hello there, first post after lurking for a while. I am experimenting with the zigfu toolbox and a kinect. I created a collision with the hand object and a trigger for basic interaction. I want to change a mapping on collision on a third object. I use two scripts that interact with each other. I used the following resource: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
The collision works fine but when i want to trigger the function in the other script it states "object not set to an instance of an object". I do however set the instance in the top of the script and it doesnt give a error on compiling that, only on runtime at the actual collision. The trigger-script is as follows:
var other : change_mapping;
other = gameObject.GetComponent("change_mapping");
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "TriggerObject"){
Debug.Log("Box went through!");
other.ChangeTex01();
}
}
I assigned the Other var to the gameobject containing the change_material script. The change_mapping script is as follows:
public var tex01_texture : MovieTexture;
public var tex02_texture : MovieTexture;
public var temp_tex : MovieTexture;
public var current_tex : MovieTexture;
function Awake() {
current_tex = temp_tex;
}
function Update () {
renderer.material.mainTexture = current_tex;
}
function ChangeTex01(){
Debug.Log("change tex01");
}
function ChangeTex02(){
Debug.Log("change tex02");
}
I feel kinda dumb because there are many posts like this but i just cant get it to work after a day of fidling and googling. Does anybody have a idea? Thanks in advance :)
is change_mapping the name of a script on the same object as the script which is trying to call it?
Im using this
var BlockPopperScript : BlockPopper;
function Update()
{
//Some Stuff
GetComponent(BlockPopper).breakBlock(blockTouched); //calls a function breakblock with args blockTouched within the script BlockPopper
}
change_mapping is the name of the script, its located on a box named cube. trigger script is located on object named RightHand @meat5000, trying your code now :)
I came up with this but it doesnt work.
var BlockPopperScript : change_mapping ;
var hit : boolean = false;
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "TriggerObject"){
Debug.Log("Box went through!");
hit = true;
}
}
function Update(){
if (hit == true){
//GetComponent(BlockPopper).breakBlock(blockTouched); //calls a function breakblock with args blockTouched within the script BlockPopper
GetComponent(change_mapping).ChangeTex01(); //calls a function breakblock with args blockTouched within the script BlockPopper
hit = false;
}
}
Its still the same, it compiles, i can run but on collision it states a error. Could it be that some kinect framework gets in the way?
If you look closely at my code you will notice that BlockPopperScript is never actually used. You can GetComponent directly from a script that is attached to the same gameobject as the script you are accessing from. Otherwise you need to Find the gameObject the script is on you want to access.
Also, the snippet was something from my game as an example, you shuld probably use your own na$$anonymous$$g :P
Answer by Hoeloe · Aug 29, 2013 at 12:16 PM
The issue is that GetComponent is not finding anything. GetComponent searches only for components attached to the current object, so if your script is on a different object (which I believe you said it was), it won't find anything. You have to specify the object the script is attached to, using something like this:
var obj : GameObject = ...//Get a reference to the object holding the script here
obj.GetComponent(change_mapping).ChangeTex01();
If my answer solved your problem, you can mark your question as "solved" by clicking the "tick" next to the answer that solved it.
Your answer
Follow this Question
Related Questions
private Vector3 function? 2 Answers
calling c# function from a js file.. 1 Answer
Accessing functions on external JS in the same object 0 Answers
what is the problem of this code ? 2 Answers
[SOLVED] Cycling all the way through weapons properly 1 Answer