Search Unity

Bug Unity update

Discussion in 'Scripting' started by Innoh265, Feb 24, 2024.

  1. Innoh265

    Innoh265

    Joined:
    Jun 19, 2019
    Posts:
    1
    I have a script help me to set shiftspeed to zero when its game over,it keeps going because it is on update under shift


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Parallaxer : MonoBehaviour
    {
    class PoolObject

    {
    public Transform transform;
    public bool inUse;
    public PoolObject(Transform t) { transform = t; }
    public void Use() { inUse = true; }
    public void Dispose()
    {
    inUse = false;
    }

    }
    public GameObject Prefab;
    public int poolSize;
    public float shiftSpeed;
    public float spawnRate;

    public YSpawnRange ySpawnRange;
    public Vector3 defaultSpawnPos;
    public bool spawnImmediate;
    public float minus;
    public Vector3 immediateSpawnPos;
    public Vector2 targetAspectRatio;
    float spawnTimer;
    float targetAspect;
    PoolObject[] poolObjects;
    GameManager game;
    [System.Serializable]
    public struct YSpawnRange
    {
    public float min;
    public float max;


    }


    void Awake()

    {
    Configure();

    }
    void Start()

    {
    game = GameManager.Instance;

    }
    void OnEnable()
    {
    GameManager.OnGameoverComfirmed += OnGameoverComfirmed;
    }

    void OnDisable()
    {
    GameManager.OnGameoverComfirmed -= OnGameoverComfirmed;

    }



    void Configure()
    {
    targetAspect = targetAspectRatio.y / targetAspectRatio.x;
    poolObjects = new PoolObject[poolSize];
    for (int i = 0; i < poolObjects.Length; i++)
    {
    GameObject go = Instantiate(Prefab) as GameObject;
    Transform t = go.transform;
    t.SetParent(transform);
    t.position = Vector3.one * 1000;
    poolObjects = new PoolObject(t);

    }
    if (spawnImmediate)
    {
    SpawnImmediate();
    }
    }

    void OnGameoverComfirmed()
    {

    for (int i = 0; i < poolObjects.Length; i++)
    {
    poolObjects.Dispose();
    poolObjects.transform.position = Vector3.one * 1000;
    }



    }
    void Spawn()
    {
    Transform t = GetPoolObject();
    if (t == null) return;
    Vector3 pos = Vector3.zero;
    pos.y = (defaultSpawnPos.x * Camera.main.aspect) / targetAspect;
    pos.x = Random.Range(ySpawnRange.min, ySpawnRange.max);
    t.position = pos;



    }

    void SpawnImmediate()
    {

    Transform t = GetPoolObject();
    if (t == null) return;
    Vector3 pos = Vector3.zero;
    pos.y = (immediateSpawnPos.y * Camera.main.aspect) / targetAspect;
    pos.x = Random.Range(ySpawnRange.min, ySpawnRange.max);
    t.position = pos;
    Spawn();

    }
    void Shift()
    {

    for (int i = 0; i < poolObjects.Length; i++)
    {
    shiftSpeed= 1 + 0.01f * Time.time;
    poolObjects.transform.localPosition -= -Vector3.down * shiftSpeed * Time.deltaTime;
    CheckDisposeObject(poolObjects);
    }



    }

    void CheckDisposeObject(PoolObject poolObject)
    {
    if (poolObject.transform.position.y < (-8 * Camera.main.aspect) / targetAspect)
    {
    poolObject.Dispose();
    poolObject.transform.position = Vector3.one * 1000;
    }
    }

    Transform GetPoolObject()
    {
    for (int i = 0; i < poolObjects.Length; i++)
    {
    if (!poolObjects.inUse)
    {
    poolObjects.Use();
    return poolObjects.transform;
    }

    }
    return null;


    }
    void Update()
    {
    minus -=.5f;
    if (game.gameOver) return;
    Shift();
    //spawnRate = 5 / ;
    //spawnRate= 5 - 0.01f;
    spawnTimer += Time.deltaTime;
    if (spawnTimer > spawnRate)
    {
    Spawn();
    spawnTimer = 0;
    }
    }
    }
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,707
    Please edit that mess and put it it in code tags, as well as explain a little more what the issue is, as frankly theres just a wall of code. "it keeps going" is vague
     
    Innoh265 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,666
    Innoh265 likes this.