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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to random Different Value - My code error or Bug ??

Discussion in 'Scripting' started by BlueRose, Jun 23, 2015.

  1. BlueRose

    BlueRose

    Joined:
    May 31, 2015
    Posts:
    4
    This script for randoming multiple prefab ^ i have double this code and get 2 multiple prefab randomly at same time but here my problem :
    1- the 1st is problem on my result is sometimes get 2 same value in random (How to get 2 a different random prefab at same times ^ random A-Random B , A not same value B)
    2- The result Sometimes fixed in horizontal angle but sometimes 2 value image fused and sometimes in Vertical position (How to get Fixed colider image in horizontal line )
    -Screen shot on Bottom-
    This preview

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SpawnMultipleOBJ_angka : MonoBehaviour {
    6.  
    7.     private Vector3 startPosition;
    8.     private float newXPos = 0f;
    9.  
    10.     public float moveSpeed = 1f;
    11.     public float moveDistance = 4f;
    12.  
    13.  
    14.     //initiate aray
    15.     public GameObject[] gameObjectSet;
    16.  
    17.     public float timeLeftUntilSpawn = 0f;
    18.     public float startTime = 0f;
    19.  
    20.     public float secondsBetweenSpawn = 3f;
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         startPosition = transform.position;
    25.     }
    26.     void SpawnRandomObject(){
    27.         int whichItem = Random.Range (0, 9);
    28.         //Debug.Log ("Our Random number is " + whichItem);
    29.         GameObject myObj = Instantiate (gameObjectSet [whichItem])as GameObject;
    30.  
    31.         myObj.transform.position = transform.position;
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update () {
    36.     //Move Distant
    37.         newXPos = Mathf.PingPong (Time.time * moveSpeed, moveDistance) - (moveDistance / 2f);
    38.         transform.position = new Vector3 (newXPos, startPosition.y, startPosition.z);
    39.         timeLeftUntilSpawn = Time.time - startTime;
    40.  
    41.         if (timeLeftUntilSpawn >= secondsBetweenSpawn){
    42.             startTime = Time.time;
    43.             timeLeftUntilSpawn = 1;
    44.  
    45.                 SpawnRandomObject();
    46.         }
    47.     }
    48. }
    49.  
     
    Last edited: Jun 23, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    [ code] [/ code] tags when you paste code into the forums please, it really helps with readability, there is a sticky at the top of the scripting forum on them...
     
    ThermalFusion and Kiwasi like this.
  3. BlueRose

    BlueRose

    Joined:
    May 31, 2015
    Posts:
    4
    Like this
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SpawnMultipleOBJ_angka : MonoBehaviour {
    6.  
    7. private Vector3 startPosition;
    8. private float newXPos = 0f;
    9.  
    10. public float moveSpeed = 1f;
    11. public float moveDistance = 4f;
    12.  
    13.  
    14. //initiate aray
    15. public GameObject[] gameObjectSet;
    16.  
    17. public float timeLeftUntilSpawn = 0f;
    18. public float startTime = 0f;
    19.  
    20. public float secondsBetweenSpawn = 3f;
    21.  
    22. // Use this for initialization
    23. void Start () {
    24. startPosition = transform.position;
    25. }
    26. void SpawnRandomObject(){
    27. int whichItem = Random.Range (0, 9);
    28. //Debug.Log ("Our Random number is " + whichItem);
    29. GameObject myObj = Instantiate (gameObjectSet [whichItem])as GameObject;
    30.  
    31. myObj.transform.position = transform.position;
    32. }
    33.  
    34. // Update is called once per frame
    35. void Update () {
    36. //Move Distant
    37. newXPos = Mathf.PingPong (Time.time * moveSpeed, moveDistance) - (moveDistance / 2f);
    38. transform.position = new Vector3 (newXPos, startPosition.y, startPosition.z);
    39. timeLeftUntilSpawn = Time.time - startTime;
    40.  
    41. if (timeLeftUntilSpawn >= secondsBetweenSpawn){
    42. startTime = Time.time;
    43. timeLeftUntilSpawn = 1;
    44.  
    45. SpawnRandomObject();
    46. }
    47. }
    48. }
    49. [/core]