I solved question on my own
How can i access another scripts GameObject Array to check its inside if there are any tower
Hello everyone im using SwitchObject method to upgrade my towers but i as i wrote the node class to give access player to when he/she presses the button of node to access shop panel and buying some towers, and when he/she bought tower i need to make sure the node can't be used again. But my IsEmpty method doesn't working and im thinking the problem is my GameObject[] array because to switch objects i created an array and stored same kind of towers inside. But even if player places turret inside of the node, in debug mode node still counts as empty, so player can buy infinite towers into the same node. Im kinda begginner in unity please help me. GameObject array for storing towers:
After player buys a tower:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurretUpgrade : MonoBehaviour
{
[SerializeField] private int upgradeInitialCost;
[SerializeField] private int upgradeCostIncremental;
public int UpgradeCost { get; set; }
public GameObject[] levels;
int current_level = 0;
private void Start()
{
UpgradeCost = upgradeInitialCost;
}
// Developer setting for upgrading turrets ( before adding upgrade button )
private void Update()
{
if (Input.GetKeyDown(KeyCode.D))
{
Upgrade();
}
}
public void Upgrade()
{
if (CurrencySystem.Instance.TotalCoins >= UpgradeCost)
{
if (current_level < levels.Length - 1)
{
current_level++;
SwitchObject(current_level);
UpdateUpgrade();
}
}
}
private void UpdateUpgrade()
{
CurrencySystem.Instance.RemoveCoins(UpgradeCost);
UpgradeCost += upgradeCostIncremental;
}
void SwitchObject(int lvl)
{
for (int i = 0; i < levels.Length; i++)
{
if (i == lvl)
{
levels[i].SetActive(true);
}
else
{
levels[i].SetActive(false);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Node : MonoBehaviour
{
// to share information which Node wants
public static Action<Node> OnNodeSelected;
public Turret Tower { get; set; }
public void SetTower(Turret tower)
{
Tower = tower;
}
// need to know if the node is empty so player can open the shop panel
public bool IsEmpty()
{
return Tower == null;
}
public void SelectTower()
{
OnNodeSelected?.Invoke(this);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Turret : MonoBehaviour
{
[SerializeField] private float attackRange = 3f;
// for storing enemies that entered range
private List<Enemy> _enemies;
// for checking if game has started
private bool _gameStarted;
// first target that entered the range
public Enemy CurrentEnemyTarget { get; set; }
private void Start()
{
_gameStarted = true;
_enemies = new List<Enemy>();
}
private void Update()
{
GetCurrentEnemyTarget();
}
private void GetCurrentEnemyTarget()
{
if (_enemies.Count <= 0)
{
CurrentEnemyTarget = null;
return;
}
CurrentEnemyTarget = _enemies[0];
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Enemy"))
{
Enemy newEnemy = other.GetComponent<Enemy>();
_enemies.Add(newEnemy);
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Enemy"))
{
// getting enemy reference
Enemy enemy = other.GetComponent<Enemy>();
if (_enemies.Contains(enemy))
{
// removing enemy from list
_enemies.Remove(enemy);
}
}
}
private void OnDrawGizmos()
{
if (!_gameStarted)
{
GetComponent<CircleCollider2D>().radius = attackRange;
}
Gizmos.DrawWireSphere(transform.position, attackRange);
}
}
these are the classes
Answer by Rawwhite · Jan 15 at 03:19 PM
Okay my brain just doesn't function right now but i solved the problem.
I just changed Tower propertys type Turret class to TurretUpgrade class and did same thing in another classes which gave an error. I know its stupid but working