Guys, it’s my first game and my question on the forum. The point is - I’m making 2D top down shooter, where enemies spawns above the screen after two second from the start and move down. Everything worked fine with this code(only one enemy prefab available),
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class GameManager : MonoBehaviour
{
public bool gameIsActive;
public bool gameOver;
public bool startGame;
private float countDownTimer;
private Enemy enemyScript;
private SpawnManager spawnManager;
private Button restartButton;
private int score;
public TextMeshProUGUI scoreText;
public GameObject enemyPref;
public GameObject loseText;
// Start is called before the first frame update
void Start()
{
score = 0;
scoreText.text = "Score:" + 0;
gameIsActive = true;
countDownTimer = 20f;
enemyScript = enemyPref.GetComponent<Enemy>();
enemyScript.enemySpeed = 1;
spawnManager = GameObject.Find("Spawn Manager").GetComponent<SpawnManager>();
}
// Update is called once per frame
void Update()
{
countDownTimer -= Time.deltaTime;
if (countDownTimer < 0)
{
countDownTimer = 20f;
ChangeDifficulty();
}
}
public void GameOver()
{
loseText.gameObject.SetActive(true);
restartButton = GameObject.FindGameObjectWithTag("RestartButton").GetComponent<Button>();
restartButton.onClick.AddListener(RestartGame);
}
void ChangeDifficulty()
{
enemyScript.enemySpeed++;
spawnManager.enemiesMaxAmount++;
} // more code down here
However, when I decided to implement 2 prefabs for enemies, I stuck : I cannot change the same component correctly inside the array of several game objects.
I cannot understand how works GetComponent in (un?not?)Instantiated prefabs
What to do the best here?