How to fetch value from other script through code in Unity? -- Unity -- fetch script

How to fetch value from other script  through code in Unity? -- Unity -- fetch script







[Ans]
target script->destination script

If I wanna fetch a value and store it into destination Script from target destination.
I have to drag GameObject which is attached to target script to destination script.

Step1:
write a target script.
Step2:
write a destionation script.
Step3:
drag the component which attach the target script into the destination script.

e.g.
If I wanna change TextListScript.num to 20 in other Script, I will do following.

In destination Script ButtonEventScript:
public class ButtonEventScript
{
public TextListScript MyScript;
MyScript.num=20;
}

In target Script  TextListScript:
public class TextListScript
{
public int  num=0;
}

And Assuming that obj is attached to TextScript.
I have to drag TextScript into MyScript field.

That's done.

Comments

Popular posts from this blog

GameObject.SetActive() v.s. GameObject.active

output message to Unity Console - Debug.Log() v.s. Debug.LogWarning() v.s. Debug.LogError() v.s. print()