Moving Unity GameObjects spawned through Instantiate

32 Views Asked by At

I am trying to make some coins I spawn trough Instantiate, to float up and down. This is the code I currently have:

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

public class Coin : MonoBehaviour{  
    public bool  isopen;

    [SerializeField]
    private float Speed = 1f;

    [Header("Sliding Configs")]
    [SerializeField]
    
    
    private Vector3 SlideDirection = Vector3.up;
    [SerializeField]
    private float SlideAmount = 1f;
    private Vector3 StartPosition;

    private Coroutine AnimationCoroutine;

    public bool diadromos = true;
    public float[] CoinR =  { 0 , 0, 0 , 0 , 0 , 0 };
    public GameObject coin;
    public GameObject spawned;
    public List<GameObject> allSpawns = new List<GameObject>();
    
     public void Start(){

        CoinR[5] = 4;
    }


     
        public void Update(){


            CoinR[0] = -51;     CoinR[1] = -32;     CoinR[2] = 1;     CoinR[3] = -24;     CoinR[4] = -20; 
         
            if (diadromos ==true){
                for(float i=0; i<= CoinR[5]; i++){
                    Invoke("SpawnCoin",0.3f);
                }
                diadromos= false;
            }

    
            foreach (GameObject thisEnemy in allSpawns){ 
            DoSlidingOpen(thisEnemy);
            DoSlidingClose(thisEnemy);
            }
        }

        public void SpawnCoin(){
            Vector3 randCoinPos = new Vector3(Random.Range(CoinR[0], CoinR[1]), CoinR[2], Random.Range(CoinR[3], CoinR[4]));
            Quaternion CoinQ = new Quaternion(90,90,0,0);
            GameObject spawned = Instantiate(coin, randCoinPos, CoinQ);
            allSpawns.Add(spawned);
        }


    private IEnumerator DoSlidingOpen(GameObject eachcoin){
        
        StartPosition = eachcoin.transform.position;
        Vector3 endPosition = StartPosition + SlideAmount * SlideDirection;
        Vector3 startPosition = eachcoin.transform.position;

        float time = 0;
        while (time < 1)
        {
            eachcoin.transform.position = Vector3.Lerp(startPosition, endPosition, time);
            yield return null;
            time += Time.deltaTime * Speed;
        }
        
    }

    

    private IEnumerator DoSlidingClose(GameObject eachcoins){

        StartPosition = eachcoins.transform.position;
        Vector3 endPosition = StartPosition;
        Vector3 startPosition = eachcoins.transform.position;
        float time = 0;
        while (time < 1)
        {
            eachcoins.transform.position = Vector3.Lerp(startPosition, endPosition, time);
            yield return null;
            time += Time.deltaTime * Speed;
        }
        
    }
}

It's kind of a mess at this point, but I have tried a lot of different things. I am trying to reuse functions (DoSlidingOpen & DoSlidingClos) from another script for opening and closing doors.

Edit: What is not happening is that the coins, which do actually spawn and are added to the list, don't move at all, they don't even glitch around. I tried to use

Invoke("DoSlidingOpen(thisEnemy)",2);
Invoke("DoSlidingOpen(thisEnemy)",2);

Which didn't work as well, not sure if invoke can take variables input or not. I also tried doing foreach() inside the functions instead of in Update, also didn't work.

0

There are 0 best solutions below