vendredi 6 novembre 2020

How to make an object teleport to another object from a random list? I need implement the random object as destination point

I am a noob making a small 2d platformer. I want my coin to teleport to an GameObject from a list, so I will have around 20 places where the coin can spawn to. The only trouble I have is when my player touches the coin, it won't teleport. I guess I need to make a variable and put there the list result, and then use it, but I doubt my poor knowledge and my not native english. Thanks for reading. Get good) 7using

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

public class Coin : MonoBehaviour {

public int coinValue = 1;
GameObject[] spawnPoints;
GameObject currentPoint;`
int index;

void Start()
{
    spawnPoints = GameObject.FindGameObjectsWithTag("Coin");
    index = Random.Range(0, spawnPoints.Length);
    currentPoint = spawnPoints[index];
    print(currentPoint.name);
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Player"))
    {
        ScoreManager.instance.ChangeScore(coinValue);
         GameObject.Find("Coin").transform.position = GameObject.Find("My random object chosen before").transform.position;
    }
}

}




Aucun commentaire:

Enregistrer un commentaire