- Home /
How to get non static values from object A and inject them in a coroutine in object B ?
I made a raycast that check if a door should load a new scene. When the player click while the ray hit the door, it starts a coroutine that initiate various action to load the new scene. It works, but...
I wish to tie some non static datas to these doors so it send the player to the appropriate scene and place just by changing values in the inspector of my door object. Those datas would be a string (the name of the room), 3 coordinates (x, y, z), and a sound effect (an audiosource, then).
So, I have this very basic script that looks like that, and which I put in every door object:
using UnityEngine;
public class SceneChanger : MonoBehaviour
{
public string Location;
public int x_coordinate;
public int y_coordinate;
public int z_coordinate;
public AudioSource RoomChangeSound;
}
I can access these value from an other object, the interact script of my player, by typing something like that, below. The problem is that I can't take advantage of my coroutines this way.
void Update()
{
if (hit.collider.CompareTag("change_scene"))
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Initiate.Fade(hit.collider.GetComponent<SceneChanger>().Location, Color.black, 1.5f);
StartCoroutine(change_scene());
}
}
And my corroutine looks like that without all the useless stuff:
IEnumerator change_scene()
{
Initiate.Fade("room_string", Color.black, 1.5f); /// This line is important
while (!Fader.startedLoading)
{
yield return null;
}
transform.parent.position = new Vector3(1f, 1.06f, 4f); ///And this one too
while (Initiate.areWeFading)
{
yield return null;
}
}
So now, what I'd like to do is get the values from the first script (the coordinates and the string) when I click on a door (with my raycast), and then add these value in the two important line marked in the last script. How can I do that? Because I just can't seem to use the "hit" of my raycast in the coroutine...
Solved... Actually I just had to write it down to get the answer by myself. I was just... tired, I guess. Just put that in the interact script of the player, so it'll access the variables from another script, store them, and then you can use these new variables (NewLockX, NewRoom, etc.) in your coroutine.
if (hit.collider.CompareTag("change_scene"))
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
{
NewRoom = hit.collider.GetComponent<SceneChanger>().Location;
NewLocX = hit.collider.GetComponent<SceneChanger>().x_coordinate;
StartCoroutine(change_scene());
}
}