How do we use a canvas printed text in another script

Hi there, i wanted use a canvas printed text as my command for the player character to shoot the enemy… is that possible? if yes, how do we do that…

What do you mean by canvas printed text? Do you mean a UI TextMeshProUGUI object?

Or is the player typing in text to trigger an attack?

You’ll need to clarify what you mean.

Owh sorry, it’s a ui…a decoded message text

You still didn’t explain what you are trying to accomplish. If it’s UI, you just set the text. Are you trying to read the text? Is it suppose to be a clickable? Better explain what you are doing and want to accomplish.

Don’t treat the canvas / text as a place to store the result.

Store the result (decoded text) somewhere in your game manager, then use it to a) display in the text, and b) your other use you need.

If the term “game manager” is unfamiliar in your context, hurry to check out some Youtube tutorials on the concept. Basically it is a manager class that lives for the duration of your game, a common place for scores and other game state to be stored.

If you are comfortable with the concept as well as the lifecycle of C# objects, here are some choices for doing it in Unity:

ULTRA-simple static solution to a GameManager:

OR for a more-complex “lives as a MonoBehaviour or ScriptableObject” solution…

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

OR just make a custom ScriptableObject that has the shared fields you want for the duration of many scenes, and drag references to that one ScriptableObject instance into everything that needs it. It scales up to a certain point.

If you really insist on a barebones C# singleton, here’s a highlander (there can only be one):

And finally there’s always just a simple “static locator” pattern you can use on MonoBehaviour-derived classes, just to give global access to them during their lifecycle.

WARNING: this does NOT control their uniqueness.

WARNING: this does NOT control their lifecycle.

public static MyClass Instance { get; private set; }

void OnEnable()
{
  Instance = this;
}
void OnDisable()
{
  Instance = null;     // keep everybody honest when we're not around
}

Anyone can get at it via MyClass.Instance., and only while it exists.