Script attached on prefab; cannot attach "text" component
I have a script, call it "contact", where I calculate the speed of movement from these variables:
public static float timeDiff;
public static Vector3 reachVector;
public static double reachDistance;
public static double speed = 0.00;
public Text DisplaySpeed;
Which then creates the final variable I want, speed (confirmed through Debug.Log):
timeDiff = contactTime - SpawnGameObjects.startTime;
reachVector = (SpawnGameObjects.palmPlacementL - SpawnGameObjects.cubeVector);
reachDistance = Math.Sqrt((reachVector.x * reachVector.x) + (reachVector.y * reachVector.y) + (reachVector.z * reachVector.z));
speed = reachDistance / timeDiff;
DisplaySpeed.text = speed.ToString ("f3");
I have a canvas, with text with input as 0.00 in my scene. I attempt to call this and manipulate it in start as:
DisplaySpeed = GetComponent<Text> ();
However, a problem perhaps is that this script is attached to a prefab which "spawns" into my scene. I see that the script now takes a text variable in the inspector, but I am unable to drag the text from my canvas into it.
I've also tried to put the prefab into my scene, and then Unity allows me to attach the text, but it goes away once the next instance is spawned.
Additionally, I've tried to create a new script, and call the variables "speed" etc from this 'contact' script, but for some reason I am unable to, even though they are public static variables. This is a technique that worked for example in this exact 'contact' script, as seen above with all the calls to SpawnGameObjects
... any ideas?
It is much appreciated, Eric
Answer by theterrificjd · Dec 12, 2017 at 01:16 PM
Make sure you have "using UnityEngine.UI" in your declarations first and foremost.
Second, static should only be the variables that are singular across all instances of this class. I don't think any of your variables there really need to be static. Public should be all you need. I could be assuming wrong about what you have set up though.
If you have a GameMaster object, I sometimes like to have my objects report to my GameMaster, than have that script pass it into whatever else I need. This works well for objects that are instantiated in separate areas, but need to interact at some point.
Something like: GameMaster.GM.ReportSpeed(gameObject);
then in your GameMaster class, just have the ReportSpeed(gameObject report) function (that should have a saved reference to whatever else you need), link the two with either Get/Sets, public variable changes, and/or a return variable.
If you don't have a GameMaster (or GameController), you can google it, it's a quick 2 minute tutorial, lots of sources have covered.
Sorry if this doesn't hit the nail on the head, it's late and I'm fairly tired. Hopefully you find something useful in it though.
Thank you for the post @theterrificjd .
I am okay with using a Game$$anonymous$$aster/$$anonymous$$anager. I actually have it up and running now, thanks.
Your answer
Follow this Question
Related Questions
Get single component from canvas, instead of all components 1 Answer
Display rigidbody speed to a world space canvas text 2 Answers
Raw Image changing texture in inspector but not in game. 1 Answer
Text in UI disappears when clicked on 0 Answers
Best optimization for multiple UI Canvas from prefabs 0 Answers