Hi, so I am making a game where you spawn a tower on a block. I need to access the block that the tower is sitting on, and I am wondering how I would do that. For example, I spawn a tower (making a tower defense game) by clicking on a block. I then want my code that is a component on the tower I just spawned to access the block it spawned on. How would I do that? Thanks!
Here is the code attached to my blocks (or waypoints):
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
[SerializeField] PathFinder pathfinder;
[SerializeField] EnemySpawner enemySpawner;
[SerializeField] GameObject enemyObject;
public bool isMoving;
bool stopMoving;
// Use this for initialization
void Start()
{
isMoving = true;
PathFinder pathfinder = FindObjectOfType<PathFinder>();
var path = pathfinder.GetPath();
StartCoroutine(FollowPath(path));
}
IEnumerator FollowPath(List<Waypoint> path)
{
foreach (Waypoint waypoint in path)
{
if (isMoving == true)
{
transform.position = new Vector3(waypoint.transform.position.x, enemySpawner.enemySpawnHeight, waypoint.transform.position.z);
yield return new WaitForSeconds(1f);
}
else
{
// Do Nothing
}
}
}
}
I copy and pasted the wrong script lol. Here you you:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoint : MonoBehaviour
{
[SerializeField] Tower towerPrefab;
[SerializeField] Tower tower;
[SerializeField] Transform towers;
[SerializeField] PathFinder pathFinder;
[SerializeField] TextController textController;
public bool hasTower;
public bool towerIsLevel1;
public bool towerIsLevel2;
public bool towerIsLevel3;
[SerializeField] EnemyTrigger enemyTrigger;
[SerializeField] TowerLevel2 towerLevel2Prefab;
[SerializeField] TowerLevel3 towerLevel3Prefab;
public Tower spawnedTower;
Tower towerSpawned;
// public ok here as it is a data class
public bool isExplored = false; // ok as is a data class
public Waypoint exploredFrom;
public bool isPlaceable = true;
public bool isPath;
Vector2Int gridPos;
const int gridSize = 20;
public int GetGridSize()
{
return gridSize;
}
public Vector2Int GetGridPos()
{
return new Vector2Int(
Mathf.RoundToInt(transform.position.z / gridSize),
Mathf.RoundToInt(transform.position.x / gridSize)
);
}
void Start()
{
}
void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
CheckForUpgrade();
if (pathFinder.numberOfTowers != pathFinder.towerLimit && isPlaceable == true)
{
SpawnTower();
var updatedTowerText = textController.towersLeft = textController.towersLeft - 1;
textController.towerText.text = updatedTowerText.ToString();
}
else if (pathFinder.numberOfTowers == pathFinder.towerLimit && hasTower == false)
{
print("Tower Limit Reached!");
}
}
}
public void SpawnTower()
{
float towerX = transform.position.x;
float towerZ = transform.position.z;
Vector3 towerPos = new Vector3(towerX, 20f, towerZ);
spawnedTower = Instantiate(towerPrefab, towerPos, Quaternion.identity);
hasTower = true;
spawnedTower.transform.parent = towers;
isPlaceable = false;
pathFinder.numberOfTowers = pathFinder.numberOfTowers + 1;
towerIsLevel1 = true;
towerIsLevel2 = false;
towerIsLevel3 = false;
}
void CheckForUpgrade()
{
if (isPlaceable == false && textController.points >= tower.scoreNeededForLvl2 && isPath == false && textController.points < tower.scoreNeededForLvl3
&& towerIsLevel3 == false && towerIsLevel1 == true && towerIsLevel2 == false)
{
print("Upgraded To Level 2");
textController.points -= tower.scoreNeededForLvl2;
towerIsLevel1 = false;
towerIsLevel2 = true;
towerIsLevel3 = false;
spawnedTower.damagePerHit = towerLevel2Prefab.damagePerHit;
}
else if (isPlaceable == false && textController.points >= tower.scoreNeededForLvl3 && isPath == false && textController.points > tower.scoreNeededForLvl2
&& towerIsLevel2 == true && towerIsLevel3 == false && towerIsLevel1 == false)
{
print("Upgraded To Level 3");
textController.points -= tower.scoreNeededForLvl3;
towerIsLevel2 = false;
towerIsLevel3 = true;
spawnedTower.damagePerHit = towerLevel3Prefab.damagePerHit;
}
}
}