Static value “lost” in Unity when called on second script
I'm trying to implement a system with platforms and OntriggerExit that spawn new platforms. The objects that contain the trigger function are children to said platforms. I.e, I have 4 triggers as children in each platform. I'm trying to use the trigger object's global position to instantiate the new platform but it keeps doing so according to the prent's position. For instance, the parent (platform) is at (0,0,0). The trigger, child to the platform, is at (-15,0,0) globally. I try to add (-35,0,0) and it instantiates at (-35,0,0) instead of (-50,0,0). I do this with a static value. I obtain the Trigger position when OnTriggerExit() and then use it on a different script as follows:
public class TriggerExitN : MonoBehaviour
{
public delegate void ExitAction();
public static event ExitAction OnChunkExitedN;
public static Vector3 positionN;
private bool exited = false;
private void OnTriggerExit(Collider other)
{
Player player = other.GetComponent<Player>();
if (player != null)
{
if (!exited)
{
exited = true;
OnChunkExitedN();
positionN = gameObject.transform.position;
}
}
}
}
If I debug positionN here it returns (-15,0,0) as expected.
Then the platform generator:
public class MapGenerator : MonoBehaviour
{
public GameObject[] mapprefabs;
public GameObject[] startprefabs;
private List<GameObject> platformslist = new List<GameObject>();
public Vector3 spawnOrigin;
private Vector3 spawnPosition;
// Start is called before the first frame update
void Start()
{
platformslist.Add(startprefabs[0]);
}
void OnEnable()
{
TriggerExitN.OnChunkExitedN += PickAndSpawnN;
}
private void OnDisable()
{
TriggerExitN.OnChunkExitedN -= PickAndSpawnN;
}
void PickAndSpawnN()
{
spawnPosition = TriggerExitN.positionN + new Vector3(-35f, 0, 0f);
foreach (GameObject platform in platformslist)
if(platform.transform.position == spawnPosition)
{
return;
}
else
{
continue;
}
GameObject objectFromChunk = mapprefabs[Random.Range(0, mapprefabs.Length)];
Instantiate(objectFromChunk, spawnPosition + spawnOrigin, Quaternion.identity);
platformslist.Add(objectFromChunk);
Debug.Log(TriggerExitN.positionN);
}
If I debug poisitionN here, it returns (0,0,0).
I even tried to switch positionN to the MapGenerator script, since there's only one instance of it in the scene. But to no avail. The same behaviour happens: (-15,0,0) debugged on the TriggerExitN and (0,0,0) debbugged on MapGenerator. As new platforms are instanciated the next positions for the platforms end up being (-50,0,0) , (-85,0,0), (-100,0,0), (-135,0,0).
I know I'm missing something, I'd just like some help with it because it's driving me mad. Please disregard the rest of the code that does not concern the value and usage of positionN for it is not yet well articulated. Thank you in advance.
Never$$anonymous$$d. Rookie mistake. In my blind rage I did not notice that`OnChunkExitedN();` should come after positionN = gameObject.transform.position;
. Someone already pointed it out for me. I feel so dumb. Thank you anyways.