- Home /
problem with calling a function from THIS script, on an object that I find with THIS script.
Let's say I have a script "cubeController.js" put on an empty object.
function enparenting(){
//looking for central piece
for(var cube:Transform in slice)
if(cube.CompareTag("centralCube")) pivot = cube;
for(var child: Transform in pivot) child.parent = null;
for(var cube: Transform in slice) cube.parent = pivot;
!!! pivot.GetComponent(cubeController).RotateObject(pivot.rotation,Quaternion.Euler(pivot.rotation.eulerAngles + Vector3.forward * 90),1.0);
}
function RotateObject(startRot : Quaternion, endRot : Quaternion, rotateTime : float) { var i = 0.0; var rate = 1.0/rotateTime; while (i < 1.0) { i += Time.deltaTime * rate; transform.rotation = Quaternion.Lerp(startRot, endRot, Mathf.SmoothStep(0.0, 1.0, i)); yield; } }
I have an array of objects with no scripts on. array is called "slice" (I do) I need to find one that has a tag called "centralCube", (it works) and after the parenting is done (it works too) I need to call function "RotateObject" on this centralCube. which doesn't work.
I guess it's some kind of "call a function on an object to which the script is not attached" problem, but so far I haven't find an explanation for this "Null" error. "not set to an instance of an object". I tried changing the information passed to the rotateObject function, but with no effect. do you have an explanation?
Answer by skovacs1 · Oct 06, 2010 at 09:32 PM
To call a function:
- The function must exist
- You must have access to the function
You are calling pivot.GetComponent(cubeController) which returns the cubeController. It would seem that cubeController is a script. You then try calling a method contained in cubeController called RotateObject that takes those parameters. You cannot call a method from a script that doesn't exist in that script, so if the definition for cubeController does not contain a function definition for RotateObject, you cannot call cubeController.RotateObject as you are trying to do. You would need to define this function in the cubeController script to be called as cubeController.RotateObject as you are attempting to do.
You then define a function RotateObject in thisScript. I assume this is the function you are intending to be calling, but you just defined a function thisScript.RotateObject. To call the method in this script, you would have to attach thisScript to your object and get the instance of thisScript on that object when you call Rotate.
As an alternative to calling it as a method, you could pass the function the transform to rotate and operate on that.
//Call this with
//RotateObject(pivot, pivot.rotation,
// Quaternion.Euler(pivot.rotation.eulerAngles + Vector3.forward * 90) , 1.0);
function RotateObject(center : Transform, startRot : Quaternion,
endRot : Quaternion, rotateTime : float) {
var i = 0.0;
var rate = 1.0/rotateTime;
while (i < 1.0) {
i += Time.deltaTime * rate;
center.rotation = Quaternion.Lerp(startRot, endRot,
Mathf.SmoothStep(0.0, 1.0, i));
yield;
}
}
hmm but the example that I showed IS actually a fragment of the "cubeController" script. so it does have a RotateObject function defined inside it. can I call rotateObject - from cubeController script, which isn't added to the object that I want to rotate? can I call it on an object that doesn't havve this, or any other scripts on it, or will I have to make a "rotateObject.js" and add it to the object that I want to rotate, in order to call "rotateObject" on it?
in order to call "rotateObject" on it? ... from another script.
No, you can't call a method on a script that doesn't exist. pivot.GetComponent(cubeController) will get the cubeController attached to pivot. If there is no such script, then there's no function to call. As I've said, you can either a)attach a script with that function defined in it or b)re-write the method and pass in the thing you want to act upon.