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

Spawnpoint Instantiate Positioning Problem

Discussion in 'Scripting' started by DaRealMik, Jun 3, 2020.

  1. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    So I have 5 spawnpoints where I use their one of their's position selected at random and instantiate an item there in this script: (ignore the stuff in the Start method, it's code to make sure the spawnpoints stay in their position on different screens)
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Spawner : MonoBehaviour {
    4.  
    5.     public Transform[] spawnPoints;
    6.     public GameObject[] randomPrefab;
    7.     public float spawnDelay = 4f;
    8.     public float spawnTimeDifference = 2f;
    9.     public RectTransform spawnpointYReference;
    10.     public RectTransform spawnpointXReference;
    11.     public RectTransform spawnpointXReference1;
    12.     public RectTransform spawnpointXReference2;
    13.     public RectTransform spawnpointXReference3;
    14.     public RectTransform spawnpointXReference4;
    15.  
    16.     void Start()
    17.     {
    18.         spawnPoints[0].position = new Vector2(spawnpointXReference.position.x, spawnpointYReference.position.y + 85f);
    19.         spawnPoints[1].position = new Vector2(spawnpointXReference1.position.x, spawnpointYReference.position.y + 85f);
    20.         spawnPoints[2].position = new Vector2(spawnpointXReference2.position.x, spawnpointYReference.position.y + 85f);
    21.         spawnPoints[3].position = new Vector2(spawnpointXReference3.position.x, spawnpointYReference.position.y + 85f);
    22.         spawnPoints[4].position = new Vector2(spawnpointXReference4.position.x, spawnpointYReference.position.y + 85f);
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if (Time.time >= spawnDelay)
    28.         {
    29.             spawnObjects();
    30.             spawnDelay = Time.time + spawnTimeDifference;
    31.         }
    32.  
    33.     }
    34.  
    35.  
    36.     void spawnObjects()
    37.     {
    38.         int randomIndex = Random.Range(0, spawnPoints.Length);
    39.         int randomNumber = Random.Range(0, 5);
    40.         for (int i = 0; i < spawnPoints.Length; i++)
    41.         {
    42.             if (randomIndex == i)
    43.             {  
    44.                 GameObject tmp = Instantiate(randomPrefab[randomNumber], spawnPoints[i].position, Quaternion.identity);
    45.                 tmp.transform.SetParent(transform, false);
    46.             }
    47.         }
    48.  
    49.        
    50.  
    51.  
    52.     }
    53.  
    54.    
    55.  
    56.    
    57.  
    58.    
    59.  
    60.  
    61.  
    62.  
    63.  
    64.  
    65. }
    66.  
    This works perfectly until I change the screen size. Then this happens:
    upload_2020-6-3_11-48-23.png

    As you can see the spawnpoint is where it should be, however my objects refuse to spawn at their position. Thanks.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You're using RectTransforms. That means they're part of the UI. The UI scales automatically when the screen size changes. If you want constant world positions, create objects for them that are not part of the UI hiearchy.
     
  3. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    The actual spawnpoints are regular Transforms, but I have a RectTransform on the UI to position them correctly. The spawnpoints aren't part of the UI.
     
  4. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    Hello?
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    It's hard to tell what the problem is from your image. I don't have the context about your game to understand what I'm looking at. Can you explain what we're looking at and what the desired outcome is?

    Given that you're positioning your objects based on UI elements, it makes sense that they would move when the screen size changes. That's the point of UI layouts.
     
  6. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    Well, I have a game where you control a bucket collecting money within 60 seconds. The spawnpoints are where that money is supposed to instantiate, but when the screen size changes the money doesn't spawn on the spawnpoint's position. About the UI elements, the money is part of the UI, and I spawnpoints are not, but I don't think that would change where they instantiate, but please correct me if I'm wrong. Thanks.
     
  7. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    Hello?