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?
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;
}
}