- Home /
is not a part of transform/not set to an instance of an objet error
There's this bit of code I can't help myself with
script name: easearoundease.js :
var sliceCubes:Transform[];
var centralCube:Transform; var axisOfARotation = Vector3.forward; var rotationAmount = 90; var howLongRotation= 1.0;
function Update () { if(Input.GetMouseButtonDown(0)){ for(var cube : Transform in sliceCubes) cube.GetComponent(rotatearoundease).RotateObject(centralCube.position, axisOfARotation, rotationAmount, howLongRotation); } if(Input.GetMouseButtonDown(1)){} //~ for(var cube : Transform in sliceCubes) cube.RotateObject(centralCube.position, -axisOfARotation, rotationAmount, howLongRotation); }
function RotateObject(point : Vector3, axis : Vector3, rotateAmount : float, rotateTime : float) { var step : float = 0.0; //non-smoothed var rate : float = 1.0/rotateTime; //amount to increase non-smooth step by var smoothStep : float = 0.0; //smooth step this time var lastStep : float = 0.0; //smooth step last time while(step < 1.0) { // until we're done step += Time.deltaTime rate; //increase the step smoothStep = Mathf.SmoothStep(0.0, 1.0, step); //get the smooth step transform.RotateAround(point, axis, rotateAmount (smoothStep - lastStep)); lastStep = smoothStep; //store the smooth step yield; } //finish any left-over if(step > 1.0) transform.RotateAround(point, axis, rotateAmount * (1.0 - lastStep)); }
It's basically a code for Rubik's Cube attempt. for the project related to this thread: http://answers.unity3d.com/questions/22375/rubiks-cube-problem
but as you may see there's always a problem with 13th line.
if(Input.GetMouseButtonDown(0)){
for(var cube : Transform in sliceCubes) cube.GetComponent(rotatearoundease).RotateObject(centralCube.position, axisOfARotation, rotationAmount, howLongRotation);
}
I tried putting
if(Input.GetMouseButtonDown(0)){
for(var cube : Transform in sliceCubes) cube.GetComponent(rotatearoundease).RotateObject(GetComponent(rotatearoundease).centralCube.position, GetComponent(rotatearoundease).axisOfARotation, GetComponent(rotatearoundease).rotationAmount, GetComponent(rotatearoundease).howLongRotation);
}
thinking that "cube" might not know what's in THIS script, but no. doesn't help.
Does anyone see an error here? Thank you for help.
Answer by Mike 3 · Oct 05, 2010 at 11:52 PM
The code looks a bit too complicated as you have it - declare the array as:
var sliceCubes : rotatearoundease[];
and things become much easier - no more GetComponent necessary, so no chance of screwing up there, and you still just drag the object on the same way as before (you don't need to drag via the component itself, for example)
On top of that, it stops you dragging on transforms which don't have your script, which is a very good thing
Note: It's best to remove all entries from the array and re-add them fresh, otherwise things get a bit iffy
Your answer
