- Home /
Script is working incorrectly. (rotation)
I have a simple script, that places copies of a prefab object on the scene at a certain order.
To the prefab is attached the following script:
var x:float = 0.0;
var y:float = 90.0;
function Start ()
{
var z:int;
z=Random.Range(1.0, 4.0);
x=(y*z);
transform.Rotate(0.0, x, 0.0);
print(x);
}
function Update ()
{
}
This script is supposed to turn the object by 90 degrees on y axis up to four times on start, in order to randomize the placement, but keeping the structure.
Problem is, when I launch the placement script, those prefabs that are turned are placed out of their place in structure.
Prefab consists of a three cubes, that are combined in an empty object.
Here is a placement script, just in case. It works fine by itself. It creates a grid of prefabs, placed in a square with a changeable distance between them and the size of a square itself.
var Xvalue:float = 0.0;
var Zvalue:float = 0.0;
var ChangeCounter:int = 1.0;
var ChangeAmount:float = 20.0;
var Technical_Couter:int = 1.0;
var PlayFieldSize:float = 0.0;
var prefab1:Transform;
var a:float=1.0;
var b:float=1.0;
function Update ()
{
if(ChangeCounter<PlayFieldSize)
{
Xvalue=(ChangeAmount*ChangeCounter);
Instantiate (prefab1, Vector3(Xvalue, 0.0, 0.0), Quaternion.identity);
print("Starting a new circle, placing first platform");
//On the first circle x=0
if(ChangeCounter>0)
{
while(Technical_Couter<(ChangeCounter+1))
{
Zvalue=(ChangeAmount*Technical_Couter);
Instantiate (prefab1, Vector3(Xvalue, 0.0, Zvalue), Quaternion.identity);
print("vertical row");
print(a);
a++;
Technical_Couter++;
}
while(Xvalue>0)
{
Xvalue=(Xvalue-ChangeAmount);
Instantiate (prefab1, Vector3(Xvalue, 0.0, Zvalue), Quaternion.identity);
print("horizontal row");
print(b);
b++;
}
}
a=1.0;
b=1.0;
ChangeCounter++;
Technical_Couter=1.0;
}
}
Answer by hvilela · Oct 26, 2012 at 12:59 AM
Looks like you have a pivot problem. Drag your prefab to the scene and select the GameObject that are parent of the three cubes. Make sure your in "pivot mode".
The gizmo will indicates your object pivot, that represents his center. He'll rotate around this. Change your Y directly in the inspector to understand it better.
Thank you, that was a very stupid mistake) I completely forgot about that setting.
Your answer
Follow this Question
Related Questions
rot not working for 2nd prefab 2 Answers
Random Script Failure 1 Answer
Can't add a script to a prefab script: JavaScript 1 Answer
Prefab Instancing 2 Answers
Prefabs and variables 1 Answer