- Home /
Question by
HelpMe22 · Oct 29, 2017 at 03:54 PM ·
gameobjectmodeltransform.translate
Model not moving when code tells it to move.
So I tried to make it so that when an int hits a specific number, it does something.
public class ObjectSpawning : MonoBehaviour {
public GameObject H1;
public GameObject H2;
public GameObject H3;
int TheNumber;
private void Start()
{
int TheNumber = Random.Range(1, 5);
if (TheNumber == 1)
{
print("a");
H1.transform.Translate(0f, 34f, 0f);
H3.transform.Translate(0f, 34f, 0f);
}
if (TheNumber == 2)
{
print("b");
H2.transform.Translate(0, 34f, 0f);
}
if (TheNumber == 3)
{
print("c");
H1.transform.Translate(0, 34f, 0f);
H3.transform.Translate(0f, 34f, 0f);
}
if (TheNumber == 4)
{
print("d");
H2.transform.Translate(0, 34f, 0f);
}
}
}
So H3 is a model of a pipe made in magicavoxel. But it doesn't move. The pipe is also an obj file.
Comment
The Start()
function runs the code only once when the script is enabled. You need to put your code in Update()
function so that code runs every frame. Read more about the Execution Order of Event Function
I also suggest taking a look at Unity's official scripting tutorials.
And if you actually intend on making it move once, then you can use:
transform.position = new Vector3(floatX, floatY, floatZ);
You already declared TheNumber here:
int TheNumber;
There is no need to declare it again. Just remove the int from line 11:
TheNumber = Random.Range(1, 5);
And do not forget to attach the script to an object.