The script is attached to an input field. I have a sphere that I have a script for that assigns values to the sphere. I want to be able to access the sphereName variable from the input field script within the sphere’s script. I have no idea how to go about this, and I’ve looked for and tried other’s solutions. Any sort of help would be appreciated, from an example of a script to pointing me towards docs.
The UI (nameInput) will need a reference to the sphere instance in order to set its name.
Easiest way to do this is to use FindObjectOfType to find the sphere instance and to get access at its properties from within nameInput. Just make sure to use FindObjectOfType in Start() and not in Awake().
If you have more than one instance of the sphere in the scene, then you’re likely going to need to link to them directly in the inspector for each sphere-inputfield pair.
The spheres are prefabs that are spawned within the scene after a certain action is completed and there can be multiple instances of the sphere. Would it still be possible to give them each their own name?
OK, I don’t know enough about your UI design and whether you want a single or multiple instances of an input field showing. I’m willing to bet you don’t need more than one instance of an InputField.
I’m going to assume that you have multiple spheres in the scene, and you tap on one to edit its name. Does that sound about right?
You’re probably going to want a third script to tie this together. Call it SphereManager. When you tap a sphere, the sphere tells the SphereManager that it’s the currently selected sphere.
Have the SphereManager contain an event called OnSphereSelected, and have the InputField listen to this event. Whenever this event fires, the InputField gets the sphere instance from the event and knows which sphere it’s setting a name for.
obviously using Interlace to interacet with other scripts through UI is a best way to communicate, since you don’t
need to know the targets script name. Using Find is slow and not recommended. Another way is to have a variable like this public MonoBehavoir OtherScript;
and then dragg that script/ObjectSphere in through the editor.