Weedosaurus
Weedosaurus

Reputation: 164

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 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.

Upvotes: 2

Views: 116

Answers (1)

vasmos
vasmos

Reputation: 2586

OnChunkExitedN();

positionN = gameObject.transform.position;

Just switch these two you are calling onchunkexitedn before setting positionN

positionN = gameObject.transform.position;

OnChunkExitedN();

Upvotes: 2

Related Questions