Например, в моей игре я хочу, чтобы эти маленькие значки игроков становились прозрачными после смерти игрока. У меня уже есть функция с именем PlayerDead
, но когда я помещаю в нее игровой объект, я не могу получить доступ к средству визуализации спрайтов. Я бы предпочел не прикреплять мой основной скрипт к иконке, но я действительно понятия не имею, как это сделать. Для справки: _checkForGameOver
- счетчик, показывающий, сколько раз погиб игрок / враг. Когда игрок умирает, на месте, где иконка уничтожена, ставится вопросительный знак, и иконка создается в этом месте. Вот часть кода из основного скрипта:
public GameObject[] playerPrefab;
public GameObject[] enemyPrefab;
public GameObject[] icon;
public GameObject[] questionMarks;
public Transform playerSpawn;
public Transform enemySpawn;
public Transform topSpawn;
public Transform middleSpawn;
public Transform bottomSpawn;
public Transform topSpawnEnemy;
public Transform middleSpawnEnemy;
public Transform bottomSpawnEnemy;
Unit playerUnit;
Unit enemyUnit;
int randomIntPlayer;
int randomIntEnemy;
public bool playerPassDamage;
public BattleHUD playerHUD;
public BattleHUD enemyHUD;
public BattleState state;
public Button attackButton;
public Button passButtonDamage;
public Button healButton;
public Button passButtonResistance;
public GameObject playAgain;
public GameObject circleAttack;
public GameObject squareAttack;
public IconColor iconColor;
public Shake shake;
private int _checkForGameOverPlayer { get; set; } = 0;
private int _checkForGameOverEnemy { get; set; } = 0;
void Start()
{
shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();
state = BattleState.START;
StartCoroutine(SetupBattle());
}
IEnumerator SetupBattle()
{
randomIntPlayer = Random.Range(0, playerPrefab.Length);
GameObject playerGO = Instantiate(playerPrefab[randomIntPlayer], playerSpawn);
playerUnit = playerGO.GetComponent<Unit>();
if(randomIntPlayer == 0)
{
Instantiate(icon[0], topSpawn);
}
else if (randomIntPlayer == 1)
{
Instantiate(icon[1], topSpawn);
}
else if (randomIntPlayer == 2)
{
Instantiate(icon[2], topSpawn);
}
else if (randomIntPlayer == 3)
{
Instantiate(icon[3], topSpawn);
}
randomIntEnemy = Random.Range(0, enemyPrefab.Length);
GameObject enemyGO = Instantiate(enemyPrefab[randomIntEnemy], enemySpawn);
enemyUnit = enemyGO.GetComponent<Unit>();
if (randomIntEnemy == 0)
{
Instantiate(icon[0], topSpawnEnemy);
}
else if (randomIntEnemy == 1)
{
Instantiate(icon[1], topSpawnEnemy);
}
else if (randomIntEnemy == 2)
{
Instantiate(icon[2], topSpawnEnemy);
}
else if (randomIntEnemy == 3)
{
Instantiate(icon[3], topSpawnEnemy);
}
playerUnit.GetComponentInChildren<SpriteRenderer>().enabled = true;
enemyUnit.GetComponentInChildren<SpriteRenderer>().enabled = true;
playerHUD.SetHUD(playerUnit);
enemyHUD.SetHUD(enemyUnit);
playerPassDamage = false;
didPlayerHeal = false;
if (playerUnit.speed > enemyUnit.speed)
{
state = BattleState.PLAYERTURN;
PlayerTurn();
}
else if (enemyUnit.speed > playerUnit.speed)
{
passButtonDamage.interactable = false;
attackButton.interactable = false;
healButton.interactable = false;
passButtonResistance.interactable = false;
state = BattleState.ENEMYTURN;
yield return new WaitForSeconds(1.2f);
StartCoroutine(EnemyTurn());
}
}
public IEnumerator EnemyDead()
{
_checkForGameOverEnemy++;
if (_checkForGameOverEnemy == 1)
{
if (randomIntEnemy == 0)
{
}
else if (randomIntEnemy == 1)
{
}
else if (randomIntEnemy == 2)
{
}
else if (randomIntEnemy == 3)
{
}
}
if (_checkForGameOverEnemy == 3)
{
state = BattleState.WON;
EndBattle();
}
yield return new WaitForSeconds(.25f);
randomIntEnemy = Random.Range(0, enemyPrefab.Length);
GameObject enemyGO = Instantiate(enemyPrefab[randomIntEnemy], enemySpawn);
enemyUnit = enemyGO.GetComponent<Unit>();
if (_checkForGameOverEnemy == 1)
{
Destroy(questionMarks[2]);
if (randomIntEnemy == 0)
{
Instantiate(icon[0], middleSpawnEnemy);
}
else if (randomIntEnemy == 1)
{
Instantiate(icon[1], middleSpawnEnemy);
}
else if (randomIntEnemy == 2)
{
Instantiate(icon[2], middleSpawnEnemy);
}
else if (randomIntEnemy == 3)
{
Instantiate(icon[3], middleSpawnEnemy);
}
}
if (_checkForGameOverEnemy == 2)
{
Destroy(questionMarks[3]);
if (randomIntEnemy == 0)
{
Instantiate(icon[0], bottomSpawnEnemy);
}
else if (randomIntEnemy == 1)
{
Instantiate(icon[1], bottomSpawnEnemy);
}
else if (randomIntEnemy == 2)
{
Instantiate(icon[2], bottomSpawnEnemy);
}
else if (randomIntEnemy == 3)
{
Instantiate(icon[3], bottomSpawnEnemy);
}
}
enemyHUD.SetHUD(enemyUnit);
if (playerUnit.speed > enemyUnit.speed)
{
state = BattleState.PLAYERTURN;
PlayerTurn();
}
else if (enemyUnit.speed > playerUnit.speed)
{
passButtonDamage.interactable = false;
attackButton.interactable = false;
healButton.interactable = false;
passButtonResistance.interactable = false;
yield return new WaitForSeconds(1f);
state = BattleState.ENEMYTURN;
StartCoroutine(EnemyTurn());
}
}
IEnumerator PlayerDead()
{
_checkForGameOverPlayer++;
if (_checkForGameOverEnemy == 1)
{
if (randomIntPlayer == 0)
{
}
else if (randomIntPlayer == 1)
{
}
else if (randomIntPlayer == 2)
{
}
else if (randomIntPlayer == 3)
{
}
}
if (_checkForGameOverPlayer == 3)
{
state = BattleState.LOST;
EndBattle();
}
yield return new WaitForSeconds(.25f);
randomIntPlayer = Random.Range(0, playerPrefab.Length);
GameObject playerGO = Instantiate(playerPrefab[randomIntPlayer], playerSpawn);
playerUnit = playerGO.GetComponent<Unit>();
if (_checkForGameOverPlayer == 1)
{
Destroy(questionMarks[0]);
if (randomIntPlayer == 0)
{
Instantiate(icon[0], middleSpawn);
}
else if (randomIntPlayer == 1)
{
Instantiate(icon[1], middleSpawn);
}
else if (randomIntPlayer == 2)
{
Instantiate(icon[2], middleSpawn);
}
else if (randomIntPlayer == 3)
{
Instantiate(icon[3], middleSpawn);
}
}
if (_checkForGameOverPlayer == 2)
{
Destroy(questionMarks[1]);
if (randomIntPlayer == 0)
{
Instantiate(icon[0], bottomSpawn);
}
else if (randomIntPlayer == 1)
{
Instantiate(icon[1], bottomSpawn);
}
else if (randomIntPlayer == 2)
{
Instantiate(icon[2], bottomSpawn);
}
else if (randomIntPlayer == 3)
{
Instantiate(icon[3], bottomSpawn);
}
}
playerHUD.SetHUD(playerUnit);
if (playerUnit.speed > enemyUnit.speed)
{
state = BattleState.PLAYERTURN;
PlayerTurn();
}
else if (enemyUnit.speed > playerUnit.speed)
{
passButtonDamage.interactable = false;
attackButton.interactable = false;
healButton.interactable = false;
passButtonResistance.interactable = false;
yield return new WaitForSeconds(1f);
state = BattleState.ENEMYTURN;
StartCoroutine(EnemyTurn());
}
}