Changing text via script

I know this has been asked and answered a 100 times but I can’t quite seem to get it, and the answers seem to be different.

I’ve created some text in the scene, it’s called scoreText. Now some answers on here say create a script, with public GUIText scoreText variable, then drag the text onto the variable in the inspector, but that doesn’t seem to work because when I select the script to drag it on, then I select the text to drag it on, the inspector changes and there’s nowhere to drag it onto.

Another answer on here says use GetComponent to find it, but that seems to be for an object called Text? and not GUIText?

Can someone just tell me how I can get to update my text via another script (not one attached to it), thanks!

First of all, create a public variable like you did before, use one of these two, depending wether you’re using the legacy GUIText or the new UI.

// Legacy
public GUIText scoreText = null;

// New UI
public UnityEngine.UI.Text scoreText = null;

After this you select the gameObject that has these properties. You should see the variable in the inspector.
Now you say it’s disappearing, which can be avoided. Either click and drag in one go, don’t click and click-drag, or lock the inspector by clicking on the lock icon in the top-right corner of the inspector. This will lock the current inspector, don’t forget to unlock it after you are done, or you won’t be able to inspect other elements.

After this you can drag the text object onto your variable and it will be assigned.

To change the text use this code

scoreText.text = "Your custom text";