Search Unity

Moving instantiated objects: what is wrong?

Discussion in 'Physics' started by fermas, Sep 26, 2015.

  1. fermas

    fermas

    Joined:
    Sep 21, 2015
    Posts:
    15
    I am trying to do a fairly simple thing, and still failing miserably. I want to randomly generate cubes across a range of space-points and the keep changing their position randomly too. I achieve the first part, creating them, but not the second, which is changing their position. Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyExample: MonoBehaviour {
    5.  
    6.     private GameObject[] objectlist;
    7.     public GameObject mycube;
    8.     private float x, y, z;
    9.  
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.         for(int i=0; i<100; i++){
    16.             x = Random.Range(-1000F, 1000F);
    17.             y = Random.Range(-1000F, 1000F);
    18.             z = Random.Range(-1000F, 1000F);
    19.  
    20.             GameObject newobj = Instantiate(mycube, new Vector3(x,y,z), Quaternion.identity) as GameObject;
    21.             newobj.tag = "Test";
    22.         }
    23.         objectlist = GameObject.FindGameObjectsWithTag("Test");
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.         foreach(GameObject obj in objectlist)
    29.         {
    30.             x = Random.Range(-1000F, 1000F);
    31.             y = Random.Range(-1000F, 1000F);
    32.             z = Random.Range(-1000F, 1000F);
    33.             obj.transform.position =new Vector3(x,y,z);
    34.         }
    35.     }
    36. }
    Does anyone know why this code is not making the instantiated cubes change their position randomly, as expected? Many thanks!
     
  2. UsulPro

    UsulPro

    Joined:
    Sep 5, 2015
    Posts:
    2
    Hi, I just check your code and everything works well. May be it's problem with scale or camera?