0

Been running into a problem that seems to keep a temp variable from being assigned and instantiated. I believe it has to do with the If statement, when I remove the condition, it'll instantiate the prefab every frame so I believe the issue lies within the If statement itself

This is the full script:

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

public class BPMinstantiator : MonoBehaviour
{
    public GameObject bpmOB;
    public GameObject bpmClone;

    public Conductor script;
    public Conductor offset;
    AudioSource song;

    float lastbeat; //this is the 'moving reference point'
    float bpm = 140;
    float dsptimesong;

    void Start()
    {

        Conductor script = bpmOB.GetComponent<Conductor>();
        song = GetComponent<AudioSource>();
        Conductor offset = GetComponent<Conductor>();
        lastbeat = 0;
        script.crotchet = 60 / bpm;

        dsptimesong = (float)AudioSettings.dspTime;
        script.songposition = (float)(AudioSettings.dspTime - dsptimesong); //*song.pitch - offset;
    }

    void Update()
    {
        Debug.Log("update start");
        if (script.songposition > lastbeat + script.crotchet) //Issue here, removal fixes issue but causes clones to instantiate every frame
        {
            Debug.Log("If statement worked");
            bpmClone = Instantiate(bpmOB, new Vector2(0f, 0f), Quaternion.identity);
            Debug.Log("clone is assigned");
            lastbeat += script.crotchet;
        }
    }
}

This is the specific region I'm looking into at the moment:

void Update()
    {
        Debug.Log("update start");
        if (script.songposition > lastbeat + script.crotchet) //Issue here, removal fixes issue but causes clones to instantiate every frame
        {
            Debug.Log("If statement worked");
            bpmClone = Instantiate(bpmOB, new Vector2(0f, 0f), Quaternion.identity);
            Debug.Log("clone is assigned");
            lastbeat += script.crotchet;
        }
    }
Noodle
  • 9
  • 1

0 Answers0