- Home /
Function deleting string by itself.
I'm trying to pass a string off to another object after instantiating it. It passes the string off correctly, but as soon as I try to use the passed string it's as if I haven't passed anything at all.
Here is the script that is passing the string off to the other script. sceneManager sceneMan;
public playerController player;
public GameObject loaderTrigger;
[SerializeField]
public string scene1ToUnload;
[SerializeField]
public string scene2ToUnload;
void Start()
{
sceneMan = FindObjectOfType<sceneManager>();
player = FindObjectOfType<playerController>();
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
if (!string.IsNullOrEmpty(scene1ToUnload) || !string.IsNullOrEmpty(scene2ToUnload))
{
sceneMan.inGameUnloadLevelAsync(scene1ToUnload, scene2ToUnload);
Instantiate(loaderTrigger, gameObject.transform.position, Quaternion.identity);
}
if (!string.IsNullOrEmpty(scene1ToUnload))
{
}
StartCoroutine(destoryObject());
}
}
private IEnumerator destoryObject()
{
loaderTrigger.GetComponent<loadSection>().recieveAreaNames(scene1ToUnload, scene2ToUnload);
yield return new WaitForSecondsRealtime(1.5f);
Destroy(gameObject);
}
Then this is the code for the script receiving the passed string: sceneManager sceneMan;
public playerController player;
public GameObject unloaderTrigger;
[SerializeField]
public string scene1ToLoad;
[SerializeField]
public string scene2ToLoad;
[SerializeField]
public string scene3ToLoad;
[SerializeField]
public string scene4ToLoad;
void Start()
{
sceneMan = FindObjectOfType<sceneManager>();
player = FindObjectOfType<playerController>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
if (!string.IsNullOrEmpty(scene1ToLoad) || !string.IsNullOrEmpty(scene2ToLoad) || !string.IsNullOrEmpty(scene3ToLoad) || !string.IsNullOrEmpty(scene4ToLoad))
{
sceneMan.inGameLoadLevelAsync(scene1ToLoad, scene2ToLoad, scene3ToLoad, scene4ToLoad);
Instantiate(unloaderTrigger, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
public void recieveAreaNames(string area1, string area2)
{
Debug.Log("Received " + area1);
area1 = scene1ToLoad;
Debug.Log("scene1ToLoad is now filled with " + area1);
}
Debug.Lost("Received " + area1) displays the correct passed string (area1) but then Debug.Log("scene1ToLoad is now filled with " + area1); reads as if area1 is empty even though it said immediately before that it was filled.
Despite not changing any code in either script at all, for some reason, it works correctly only sometimes.
What?
Seems that you are doing the assignment the wrong way?
area1 = scene1ToLoad; //you set area1 to be equal to scene1ToLoad
scene1ToLoad = area1; //you set scene1ToLoad to be equal to area1
I am a colossal idiot. Thank you so much for answering this!