N
N
nika092017-06-14 16:35:15
C++ / C#
nika09, 2017-06-14 16:35:15

How to use a variable from one script in another?

For example, there are two scenes, Scene1 and Scene2
In Scene1 there is a Script1 script, which has a root variable, weighs on the rt object, and in Scene2 there is a Script2 script.
Can you please tell me how to use the root variable in Script2 script??

public class Test : MonoBehaviour
{
Script1 myscript;
void Start()
  {
    myscript = GameObject.Find("rt").GetComponent<Script1> ();
  }
}

This code does not work, apparently somewhere else you need to take into account the scene, please tell me how to solve this problem ??

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2017-06-14
@nika09

If both scenes are not loaded at the current moment, then you cannot knock from one to the other.
It's like trying to find a bed in the kitchen. (kitchen, bedroom - different rooms/scenes)
You either make it a static class/variable, or a non-destructible object that will remain in the scene when a new one is loaded.
Or still load both scenes additively..so that you can search in both)
Using the analogy above, you would not build a kitchen or a bedroom. and the house from the kitchen and bedroom, and there they would already be looking for their bed in them)

D
Di-Roll, 2017-06-14
@Di-Roll

In theory, the code should work if the root variable is public and there is an rt object on the stage that has a Script1 component.
answers.unity3d.com/questions/42843/referencing-no...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question