I’m making a mobile AR educational app that has to show how a electric current “moves”. I would use the log for testing it but I can’t run the tests without building
This instantiates the electricity effect and the path that it has to follow.
The code:
public class divergeCheck : MonoBehaviour
{
public GameObject divergeTrigger; //detects when to instantiate the object
public GameObject deleteTrigger;//detects when to delete the object
public GameObject electricCurrent;
public GameObject electricPrefab;
public GameObject pathCreator;
private GameObject instance;
private GameObject pathInstance;
private Vector2 deletePosition; //to handle the position of the divergeTrigger
private Vector2 startPosition; //to handle the position of the deleteTrigger
private Vector2 currentPosition; //to handle the position of the current
void Start()
{
//null check..........
if (divergeTrigger == null || deleteTrigger == null || electricCurrent == null || electricPrefab == null)
{
Debug.LogError("One or more required GameObjects are not assigned!");
return;
}
//....................
startPosition = new Vector2(divergeTrigger.transform.position.x, divergeTrigger.transform.position.z);
deletePosition = new Vector2(deleteTrigger.transform.position.x, deleteTrigger.transform.position.z);
}
void Update()
{
currentPosition = new Vector2(electricCurrent.transform.position.x, electricCurrent.transform.position.z);
if (IsPositionInRange(currentPosition, startPosition) && (!(instance == electricPrefab)))
{
instance= Instantiate(electricPrefab, Vector3.zero, Quaternion.identity);
pathInstance = Instantiate(pathCreator, Vector3.zero, Quaternion.identity);
}
if (IsPositionInRange(currentPosition, deletePosition))
{
Destroy(instance);
Destroy(pathInstance);
instance = null;
pathInstance = null;
}
}
private bool IsPositionInRange(Vector2 position, Vector2 targetPosition)
{
return Mathf.Approximately(position.x, targetPosition.x) && Mathf.Approximately(position.y, targetPosition.y);
}
}
Sorry for leaving the entire code, but I have no idea where the error comes from and I’m too lazy to test it in another enviroment.