I have a prefab of a box, called box, and i want to get all 5 of them and change a variable within their script. Right now i have it find one of the boxes every cycle and change it but it didn’t work.
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
[SerializeField] Transform car;
private Camera mainCamera;
private bool inMap = false;
public Van van;
public GameObject Box;
void Start()
{
mainCamera = Camera.main;
}
void LateUpdate()
{
Box = (GameObject)FindObjectOfType(typeof(Boxer));
if (Input.GetKey(KeyCode.D) && transform.position.x < 20)
{
transform.position = new Vector3(transform.position.x + 25 * Time.unscaledDeltaTime, transform.position.y, -10);
}
if (Input.GetKey(KeyCode.W) && transform.position.y < 35)
{
transform.position = new Vector3(transform.position.x, transform.position.y + 25 * Time.unscaledDeltaTime, -10);
}
if (Input.GetKey(KeyCode.A) && transform.position.x > -20)
{
transform.position = new Vector3(transform.position.x - 25 * Time.unscaledDeltaTime, transform.position.y, -10);
}
if (Input.GetKey(KeyCode.S) && transform.position.y > -35)
{
transform.position = new Vector3(transform.position.x, transform.position.y - 25 * Time.unscaledDeltaTime, -10);
}
if (Input.GetKeyDown(KeyCode.M))
{
if (!inMap)
{
mainCamera.orthographicSize += 10;
inMap = true;
Time.timeScale = 0;
}
else
{
mainCamera.orthographicSize -= 10;
inMap = false;
transform.position = new Vector3(car.position.x, car.position.y, -10);
Time.timeScale = 1;
}
}
if (!inMap)
{
transform.position = new Vector3(car.position.x, car.position.y, -10);
van.inMap = false;
Box.GetComponent<Boxer>().inMap = false;
}
else
{
van.inMap = true;
// the area i want to change the variable
Box.GetComponent<Boxer>().inMap = true;
}
Debug.Log(inMap);
}
}