Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Random spawn object

Discussion in 'Scripting' started by Kraznetor, Mar 21, 2020.

  1. Kraznetor

    Kraznetor

    Joined:
    May 7, 2019
    Posts:
    23
    Hi, I successfully added a movement script in my game, a simple drag to move in all three axes, but this works only for the gameobject I select. Is there a way to randomly spawn gameobject in a delimitated area and assign that script? For example I have a square area from (0x;0z) to (10x;0z) in base and (0x;0z) to (0x;10z) and I want blocks to spawn from y=20 in that area randomly with different mass. Thanks in advance. Btw I’m willing to buy one course to learn coding on unity, any suggestion?
     
  2. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Just do the unity training on the unity site. free and good.

    You have to define a list of objects you randomly pick from.
    What I have done to create a random enemy is to create a public array on a persistent game object. Then drag some prefabs into that array. Then have a script randomly pick one of those game object from the array and instantiate a copy.

    This code does that.
    Code (CSharp):
    1. GameObject NewTurret = Instantiate(SceneScript.EnemyTurretsArray[Random.Range(0, 3)]);
    Instead of assigning the controller script at runtime, why not just put that script on the prefabs? Easier.

    But you can also assign a script to an object. Here I assign a movement script to a fragment of an exploded asteroid.
    Code (CSharp):
    1. Spawn.AddComponent<SR_AsteroidFragment>();
    Read up on AddComponent, you can add lots of things after you spawn an object, like a rigidbody or a collider.
     
    Kraznetor and Kurt-Dekker like this.
  3. Kraznetor

    Kraznetor

    Joined:
    May 7, 2019
    Posts:
    23

    I managed to create this script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Spawn : MonoBehaviour {

    public GameObject Peso1;
    public GameObject Peso2;
    public GameObject Peso3;
    public GameObject Peso4;
    public GameObject Peso5;
    public GameObject Peso6;
    public GameObject Peso7;
    public GameObject Peso8;
    public GameObject Peso9;
    public GameObject Peso10;
    private GameObject clone;
    private GameObject obj;
    int i;

    // Use this for initialization
    void Start ()
    {

    }

    void Update()

    {


    i = Random.Range(0,4);
    if(i == 0)
    {
    obj = Peso1;
    }
    else if(i == 1)
    {
    obj = Peso2;
    }
    else if(i == 2)
    {
    obj = Peso3;
    }
    else if(i == 3)
    {
    obj = Peso4;
    }

    Vector3 screenPosition = (new Vector3(Random.Range(-7,7), Random.Range(25,47), 0));
    clone = Instantiate(obj,screenPosition,Quaternion.identity);





    }
    }

    Actually i need to adjust the number of the objects but that's not the point. The spawn rate is too high, I want to set a timer for spawning or maybe let a block spawn when the clone reach certain heigh(y). Any hint?