Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by EJMVR · Dec 12, 2017 at 11:49 AM · scripting problemtextcanvasguitextstatic variable

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EJMVR · Dec 12, 2017 at 03:03 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges