VolcanicTitan
VolcanicTitan

Reputation: 121

How do I randomize an object's position?

This is some code that instantiates prefabs of two different types and places them in a random spot. The prefabs instantiate, but don't instantiate randomly. How can I fix this???

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {
// Use this for initialization
    void Start () {
        InvokeRepeating ("SpawnAThing", 3.0f, 3.0f);
    }

// Update is called once per frame
    void Update () {

    }
    void SpawnAThing(){
        GameObject x=null;
        int z = Random.Range (0, 2);
        switch (z) {
        case 0:
            x = (GameObject)Instantiate(Resources.Load("BadCircle"));;
            break;
        case 1:
            x = (GameObject)Instantiate(Resources.Load("GoodCircle"));
            break;
        }
        x.transform.position.Set (Random.Range (-Screen.width, Screen.width), Random.Range (-Screen.height, Screen.height), 0.0f);
    }
}

Upvotes: 2

Views: 3037

Answers (2)

Alan Vieira Rezende
Alan Vieira Rezende

Reputation: 412

Create a GameObject and rename it to "SpawnController" and attach this script:

 using UnityEngine;

    public class SpawnController : MonoBehaviour {

        //Set at inspector new Min and Max X axis Range.
        public float maxWidth;
        public float minWidth;

        //Set at inspector new Min and Max Y axis Range
        public float maxHeight;
        public float minHeight;

        //Set at inspector new Min and Max Z axis Range (3D game)
        public float maxDepth;
        public float minDepth;

        //Set the time at inspector you want the object to be created eg: every 10 seconds
        public float rateSpawn;

        private float currentRateSpawn;

        // Drag to inspector your gameObject what you want instatiate
        public GameObject gameObject;

        void Update () {

            currentRateSpawn += Time.deltaTime;
            if (currentRateSpawn > rateSpawn) {
                currentRateSpawn = 0;
                Spawn ();
            }
        }

        private void Spawn() {


            //Define new (Min and Max) range values for the Vector3 AXIS
            float randWitdh = Random.Range(minWidth, maxWidth);
            float randHeight = Random.Range(minHeight, maxHeight);
            float randDepth = Random.Range(minDepth, maxDepth);

            //Vector3 now has a new random range value
            Vector3 random =  new Vector3(randWitdh, randHeight, randDepth ); 

            //Object will dynamically instantiate according to the values established in the inspector
            Instantiate (gameObject, random, Quaternion.identity);
        }
    }

Upvotes: 0

Programmer
Programmer

Reputation: 125455

Your code is not working properly due to the use of transform.position.Set()

position.Set() cannot modify the position because it does not return the reference of that position. It returns a copy.

Modify the transform directly with x.transform.position = new Vector3(x,y,z);

So all you have to do is replace

x.transform.position.Set (Random.Range (-Screen.width, Screen.width), Random.Range (-Screen.height, Screen.height), 0.0f);

with

x.transform.position = new Vector3(Random.Range(-Screen.width, Screen.width), Random.Range(-Screen.height, Screen.height), 0.0f);

EDIT:

You can't see it now because Screen.width and Screen.height are way too off to the screen. You have to convert view to world point with Camera.main.ViewportToWorldPoint then you can use 0 to 1 to represent the screen with .5 being the middle point. If you don't see it, decrease 15 that is passed into the Z-axis.

  x.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Camera.main.nearClipPlane + 15f));

Upvotes: 4

Related Questions