Forum begins after the advertisement:


[Part 6] Trying to spawn the player in a randomly generated map

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 6] Trying to spawn the player in a randomly generated map

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #15191
    C Vagdalt
    Participant
    Helpful?
    Up
    0
    ::

    hello, i am trying to spawn my player from the character selector into a randomly generated map, it is currently set up so that the player goes to a loading scene so the map has time to generate, when the map has finished generating you go to the map. the problem we are having is that the player doesnt spawn on the map. i uploaded a video showing the process.

    View post on imgur.com

    help for this would be really appreciated, thanks in advance

    below i will paste scripts that we have for the load scene:

    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class LevelMove_Ref2 : MonoBehaviour
    {
        public string sceneName = "HellMap"; // Specify the scene name to load
        public WaveSpawner waveSpawner;
    
        private void OnTriggerEnter2D(Collider2D other)
        {
            if (other.CompareTag("Player"))
            {
                // Stop current wave spawning if waveSpawner is assigned
                if (waveSpawner != null)
                {
                    waveSpawner.StopAllCoroutines();
                }
    
                // Load the specified scene
                SceneManager.LoadScene(sceneName);
            }
        }
    
        private void Start()
        {
            waveSpawner = FindObjectOfType<WaveSpawner>();
            if (waveSpawner == null)
            {
                Debug.LogError("WaveSpawner not found in the scene.");
            }
        }
    }
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class SceneLoader : MonoBehaviour
    {
        public string[] scenes;
    
        public void LoadLoaderScene()
        {
            SceneManager.LoadScene("Load Scene");
        }
    
        public void LoadRandomScene()
        {
            if (scenes.Length > 0)
            {
                string randomScene = scenes[Random.Range(0, scenes.Length)];
                SceneManager.LoadScene(randomScene);
            }
            else
            {
                Debug.LogError("No scenes assigned to SceneLoader.");
            }
        }
    
        private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
        {
            // Find and initialize the player and wave spawner in the new scene
            WalkerGenerator walkerGenerator = FindObjectOfType<WalkerGenerator>();
            if (walkerGenerator != null)
            {
                walkerGenerator.SpawnPlayer(); // Call the SpawnPlayer method
            }
            else
            {
                Debug.LogError("WalkerGenerator not found in the scene.");
            }
    
            EnemySpawner enemySpawner = FindObjectOfType<EnemySpawner>();
            if (enemySpawner != null)
            {
                enemySpawner.InitializeSpawner();
            }
        }
    
        private void OnEnable()
        {
            SceneManager.sceneLoaded += OnSceneLoaded;
        }
    
        private void OnDisable()
        {
            SceneManager.sceneLoaded -= OnSceneLoaded;
        }
    }
    #15194
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you add the following line above walkerGenerator.SpawnPlayer() and see if it prints when the player is supposed to spawn?

    print("SpawnPlayer() called");
    #15197
    C Vagdalt
    Participant
    Helpful?
    Up
    0
    ::

    these are all the messages in the console of the whole process of moving to the map. spawnplayer does get called

    View post on imgur.com

    #15198
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you share your SpawnPlayer() function?

    #15201
    C Vagdalt
    Participant
    Helpful?
    Up
    0
    ::

    this is the spawn player function inside of the walker generator

    private void SpawnPlayer()
    {
        List<Vector3Int> floorTiles = new List<Vector3Int>();
    
        for (int x = 0; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight; y++)
            {
                if (map[x, y] == TileType.Floor)
                {
                    floorTiles.Add(new Vector3Int(x, y, 0));
                }
            }
        }
    
        if (floorTiles.Count == 0)
        {
            Debug.LogError("No floor tiles available for spawning objects.");
            return;
        }
    
        Vector3Int playerSpawnPos = floorTiles[Random.Range(0, floorTiles.Count)];
        Debug.Log($"Player spawn position: {playerSpawnPos}");
    
        GameObject playerObj = Instantiate(playerPrefab, new Vector3(playerSpawnPos.x + 0.5f, playerSpawnPos.y + 0.5f, 0), Quaternion.identity);
        Debug.Log($"Player instance created at position: {playerObj.transform.position}");
    
        if (cameraMovement != null)
        {
            cameraMovement.target = playerObj.transform;
            cameraMovement.SnapToTarget();
        }
        else
        {
            Debug.LogError("CameraMovement script is not assigned in the WalkerGenerator.");
        }
    }
    #15209
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    It seems like your SpawnPlayer() function is completely not called. Did you put your print() statement inside of the if or outside of it?

    #15219
    C Vagdalt
    Participant
    Helpful?
    Up
    0
    ::

    its in between the if statements, ill post a pic of it

    View post on imgur.com

    #15224
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you check if your Console is showing error messages? If your SpawnPlayer() fails to complete, there is supposed to be a line on the Console saying that it has failed, but I don’t see this line appearing in your Console:

    Debug.LogError("No floor tiles available for spawning objects.");
    #15226
    C Vagdalt
    Participant
    Helpful?
    Up
    0
    ::

    these are all the console errors and warnings i get from my start scene all the way to my map scene

    View post on imgur.com

    #15234
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    That’s quite a long list of errors which may potentially be feeding into the error that you are facing. Do you know how to get started fixing them?

    #15297
    C Vagdalt
    Participant
    Helpful?
    Up
    0
    ::

    some of these look like easy fixes, i tried working on the index out of range error before but couldn’t find a solution for it

    #15298
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    The IndexOutOfRangeException occurs when you try to access a value in an array or list that is bigger than the length of the array / list. It usually happens when you are using array indexes.

    myArray[i];

    If the value of i is equal to or larger than the number of items in the array, then the IndexOutOfRange will occur. E.g. if you array has 4 items, and i has a value of 7.

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: