- Home /
How can the first C# script get the second C# script data?
public class First : MonoBehaviour {
public GameObject secondClass;
public Vector3 pos;
void Update () {
if (Input.GetKey(KeyCode.Space) ) { Instantiate(secondClass, pos, Quaternion.identity);
Debug.Log(secondClass.GetComponent<Second>().open); //this will print false,WHY?
//Is the "Instantiate" problem? } } }
public class Second : MonoBehaviour {
public bool open;
void Start (){ open = false; }
void Update (){
int a = 1;
if(a == 1) { open = true;
Debug.Log(open); //this will print true }
Answer by FernandoHC · Aug 16, 2018 at 05:09 PM
The reason why you don't see the value as true is because Start and Update methods will only be called on the next frame, after Instantiate is called. Therefore you must either wait for the next frame to check the value, or put the Start/Update on a public method of Second class which is called before you read "open".
Please be aware calling Start/Update before the object is properly initialized by the UI system will cause all sorts of inconsistencies when reading the objects Mono values.
Your answer
![](https://koobas.hobune.stream/wayback/20220612172935im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i make a Fps game on unity 5 3 Answers
Timeline not play animation when evaluate calling manually from script 1 Answer
Grabbing UI text as a public variable? 2 Answers
Raycasting only works when close to an object 0 Answers
Help me with this code proplem,,Almost finish my game please help this once 3 Answers