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

2D instantiation of prefab not working

Discussion in '2D' started by sjurick, Feb 28, 2014.

  1. sjurick

    sjurick

    Joined:
    Jan 2, 2013
    Posts:
    16
    I have a simple game I'm working on. Basically its targets and if you click on left-mouse button or press finger on tablet or phone, the target (which is a prefab) is supposed to destroy, then 1 gets added to score, rinse and repeat with new target appearing in a random x-axis coordinate.

    When I start the game, the target appears and if I click on it, it disappears, the score gets updated and another target gets instantiated in the same exact location, not random.

    The destruction of the target is done in a separate script which is attached to the target prefab. I've setup debug.log to let me know that a) the target has been hit, and b) that the target script is being communicated with via sendmessage. Can someone help me understand why the prefab target is not randomizing?

    I have basically this same exact code, but converted to 3D and it works fine. This is a 2D game though.

    PlayerControl Script (attached to Player game object, which is an empty GO)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerControl : MonoBehaviour {
    6.  
    7.     public GameObject target;
    8.     public GUIText scoreText;
    9.     public static int score;
    10.     public GameObject targetSpawn;
    11.     public static Vector3 position = new Vector3(Random.Range(-5,5),-1,1);
    12.  
    13.     void Awake () { }
    14.  
    15.     void Start () {
    16.         targetSpawn = Instantiate(target, position, Quaternion.identity) as GameObject;
    17.         score = 0;
    18.     }
    19.  
    20.     void Update () {
    21.         ShootObject();
    22.         SetScore();
    23.     }
    24.  
    25.     void ShootObject() {
    26.         if(Input.GetMouseButtonDown(0)) {
    27.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    28.             RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
    29.  
    30.             if (hit.transform.tag == "Target"){
    31.                 Debug.Log("Target Hit!");
    32.                 targetSpawn.SendMessage("TargetHit", SendMessageOptions.RequireReceiver);
    33.                 score ++;
    34.                 Spawner();
    35.             }
    36.         }
    37.     }
    38.  
    39.     void SetScore () {
    40.         scoreText.text = "Score: " + score;
    41.     }
    42.  
    43.     void Spawner () {
    44.         targetSpawn = Instantiate(target, position, Quaternion.identity) as GameObject;
    45.     }
    46. }
    47.  
    Target script, attached to the Target prefab

    Code (csharp):
    1.  
    2.     public void TargetHit ()
    3.     {
    4.         Debug.Log("Made it to Target.cs script");
    5.         Destroy(this.gameObject);
    6.     }
    7.  
     
  2. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    Very simply you need to randomize that vector every time in Spawner(), not just in the declaration.

    Code (csharp):
    1.  
    2. void Spawner () {
    3.  
    4.         position = new Vector3(Random.Range(-5,5),-1,1);
    5.         targetSpawn = Instantiate(target, position, Quaternion.identity) as GameObject;
    6.  
    7.     }
    8.  
     
  3. sjurick

    sjurick

    Joined:
    Jan 2, 2013
    Posts:
    16
    Thank you sir. Did the trick. For my education, why did declaring "position" at the top not expose properly to the Spawner method but it works in Start?
     
  4. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    It exposes it properly in both places, but when you're only randomizing the position once when you only have it at the top, so when you call Spawner() it was using the same position every time.