Wednesday, November 13, 2024
HomeGame Developmentunity - Gameobject not spawning in desired place after InvokeRepeating and Instantiate

unity – Gameobject not spawning in desired place after InvokeRepeating and Instantiate


It seems to be like your Subject array is storing references to the “Grass”, “Highway”, and “River” baby objects of your SpawnManager on this scene.

Meaning these objects and their youngsters like autos are a part of the energetic recreation scene, ticking and updating each body. So the unique automobile(s) begin at their respective waypoints and start driving alongside the highway. Then someday later you spawn a brand new highway, which copies the entire Highway object and its youngsters of their present state – partway alongside the highway.

This code wants some main refactoring to repair this, as a result of it is counting on these objects being within the scene to fluctuate their setups.

Proper now the one belongings you do to the copy are instantiate it and place it:

GameObject Floor = Instantiate(Subject[j].spawnField);
// ...
Floor.remodel.place = intPos;
// These two may very well be one line, by the way in which: 
// var Floor = Instantiate(Subject[j].spawnField, intPos, Quaternion.identification);

The whole lot else – VehicleToggle / TreeToggle / PlankToggle – by no means will get a reference to this Floor variable, to allow them to’t alter the newly-spawned slice of the world in any manner.

However how are you seeing variations?

As a result of they maintain modifying the originals – watch them rigorously within the Scene view whereas your recreation is working and you may see the unique world slices have their bushes, autos, and planks blinking out and in of existence. No matter state was left after the final world slice spawned is the one which’s cloned to make the subsequent world slice.

To repair this, I would advocate shifting most of this logic from the supervisor spawning the world slices, and into the world slice prefabs themselves:

public class SpawnPoint : MonoBehaviour {
    [SerializeField] Rework _prefab;

    public void Spawn() {
        Instantiate(_prefab, remodel.place, _prefab.rotation);
    }
}

public class MapChunk : MonoBehaviour {
    [System.Serializable]
    public struct SpawnCollection {
        [Range(0, 1)]
        [SerializeField] float _chancePerSpawner;
        [SerializeField] int _maxSpawns;
        [SerializeField] SpawnPoint[] _spawnPoints;

        public void Spawn() {
            int spawned = 0;
            foreach (var spawner in _spawnPoints) {
                if (spawned == _maxSpawns) break;
                if (Random.worth < _chancePerSpawner) {
                    spawner.Spawn();
                    spawned++;
                }
            }
        }
    }

    [SerializeField] int _width = 1;

    [SerializeField] SpawnCollection[] _collections;

    public Vector3 SpawnAt(Vector3 place) {
        var occasion = Instantiate(this, place, remodel.rotation);

        foreach (var assortment in occasion._collections) {
            assortment.Spawn();
        }

        place.x += _width;
        return place;
    }
}

Then your SpawnManager doesn’t must find out about grass/highway/river – it could simply have an array of MapChunk prefabs from the Property folder, not the scene.

It could actually randomly choose one, name its Spawn methodology with the present spawn place, and get again the place for the subsequent spawn, making an allowance for the width of the chosen chunk. This allows you to combine and match wider and narrower prefabs for extra complicated setups.

All of the logic of find out how to arrange the contents of a given slice is delegated to that slice – simplifying your supervisor, and making certain you do not by accident repeat the error of modifying the worldwide state / unique prefab as a substitute of the spawned occasion.

Every map slice exposes a set of associated spawners you should use to group the left vs proper lanes of visitors, and implement limits on the variety of vehicles per course or variety of bushes. Simply notice that – matching the code you supplied – spawn factors earlier within the array have the next chance of being chosen, whereas spawn factors later within the checklist are solely used if the sooner ones fail their random roll.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments