Search Unity

Systematic Spawn Position // Record User Interaction and Object Properties

Discussion in 'Scripting' started by EJMVR, Jul 26, 2017.

  1. EJMVR

    EJMVR

    Joined:
    Mar 29, 2017
    Posts:
    3
    Hi ---
    So, I am attempting to do the following:
    1. Spawn a cube at position in XY-space with fixed Z (this works)
    2. But ideally... Spawn a cube at a specific pre-defined position say position X out of 100 )
    3. Destroy cube upon click (this works)
    4. Record the position that the cube was destroyed, at what time, and with what click (this only partially works)
    5. Spawn another cube at new and unique specific position (X / 100)
    6. Repeat until all 100 positions have been recorded
    7. End
    I have now two very basic scripts. One which accomplishes #1 and half of #5. The other accomplishes #3 and a little of #4.

    I'd appreciate any scripting in C#, as I am still learning the ins-and-outs of the language.

    Scripts are attached!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpawnGameObjects : MonoBehaviour
    5. {
    6.     // public variables
    7.     public float secondsBetweenSpawning = 1f;
    8.     public float xMinRange = -3.0f;
    9.     public float xMaxRange = 3.0f;
    10.     public float yMinRange = -3.0f;
    11.     public float yMaxRange = 3.0f;
    12.     public float zMinRange = 1.0f;
    13.     public float zMaxRange = 1.0f;
    14.     public GameObject[] spawnObjects; // what prefabs to spawn
    15.  
    16.     private float nextSpawnTime;
    17.  
    18.     // Use this for initialization
    19.     void Start ()
    20.     {
    21.         // determine when to spawn the next object
    22.         nextSpawnTime = Time.time+secondsBetweenSpawning;
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update ()
    27.     {
    28.         if (!GameObject.FindGameObjectWithTag("Cube")) {
    29.  
    30.         // Spawn the game object through function below
    31.         MakeThingToSpawn ();
    32.  
    33.         // determine the next time to spawn the object
    34.         nextSpawnTime = Time.time+secondsBetweenSpawning;
    35.         }
    36.     }
    37.     void MakeThingToSpawn ()
    38.     {
    39.         Vector3 spawnPosition;
    40.  
    41.         // get a random position between the specified ranges
    42.         spawnPosition.x = Random.Range (xMinRange, xMaxRange);
    43.         spawnPosition.y = Random.Range (yMinRange, yMaxRange);
    44.         spawnPosition.z = Random.Range (zMinRange, zMaxRange);
    45.  
    46.         // determine which object to spawn (in this case, always the cube)
    47.         int objectToSpawn = Random.Range (0, spawnObjects.Length);
    48.  
    49.         // actually spawn the game object
    50.         GameObject spawnedObject = Instantiate (spawnObjects [objectToSpawn], spawnPosition, transform.rotation) as GameObject;
    51.  
    52.         // make the parent the spawner so hierarchy doesn't get super messy
    53.         spawnedObject.transform.parent = gameObject.transform;
    54.     }
    55. }
    56.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class DestroyObject : MonoBehaviour {
    6.  
    7. Ray ray;
    8. RaycastHit hit;
    9. void Update()
    10.     {
    11.         ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    12.  
    13.         if (Physics.Raycast (ray, out hit) && Input.GetMouseButtonDown (0)) {
    14.             if (hit.collider.tag == "Cube") {
    15.                 Debug.Log ("Left" + Input.mousePosition);
    16. //is there a way to get the "spawnPosition" from the prior script???
    17. //I've tried:  GameObject.Find("Cube").GetComponent<SpawnGameObjects>().spawnPosition))
    18. //but I receive the error: object reference not set to instance of an object
    19. //in theory, the mousePosition should also work, but in this case it is always in the center of the screen, so the pixel value return is always the same!
    20.                 Destroy (gameObject);
    21.             }
    22.         }
    23.         if (Physics.Raycast (ray, out hit) && Input.GetMouseButtonDown (1)) {
    24.             if (hit.collider.tag == "Cube") {
    25.                 Debug.Log ("Right" + Input.mousePosition);
    26.                 Destroy (gameObject);
    27.             }
    28.         }
    29.     }
    30. }
    Thanks for the help!
     
    Last edited: Jul 26, 2017