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 /
avatar image
0
Question by ChocolateMoose · Oct 07, 2017 at 06:41 AM · textvariablescripting beginner

How to access variables from scripts on different objects then display it on a textmesh?

Hello I've been really stumped on how I can display how much ammo a gun has on the text mesh component on the canvas gameObject. I've been searching for solutions all day but I just can't find anything. First I need to access the Ammo variable from a different script and then display that variable on a different component. I have no clue how to get variables from separate scripts on a different object. If anyone could help me with a C# example that would be awesome. My gun script is down below if anyone needs a reference, sorry if it's messy or bad

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Weapon : MonoBehaviour { public float fireRate = 0.5f; //How fast the weapon fires public int Ammo = 3; //How much ammo the weapon fires public float timeToFire = 0.0f; //used for firerate public Transform ejectPoint; //where the bullets are ejected public GameObject bullet; //the bullet object that is spawned public int ReloadAmount = 10; //how much is reloaded at a time // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (fireRate == 0 && Ammo >= 1) { //If it's 0 then it's a semi-auto if (Input.GetButtonDown ("Fire1")) { Instantiate (bullet, ejectPoint.position, ejectPoint.rotation); Ammo = Ammo - 1; } } else if (Input.GetButton ("Fire1") && Time.time >= timeToFire && Ammo >= 1) { //If it's any other positive number then it's auto timeToFire = Time.time + fireRate; Instantiate (bullet, ejectPoint.position, ejectPoint.rotation); Ammo = Ammo - 1; } if (Input.GetButtonDown ("Fire2")) { //Reloading thing Debug.Log ("Reloading..."); Ammo = Ammo + ReloadAmount; } } }

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
0

Answer by TheBleedingNova · Oct 07, 2017 at 07:23 AM

Your code isn't indented whatsoever, so it's impossible to read.

To access variables from other scripts you must make an object reference to the script you are trying to access.

Example:

 public NameOfYourScript variableName;
 
 NameOfYourScript.VariableOnThatScript  = something;

You would assign NameOfYourScript in the inspector on the object that the script is attached to. Then you can access any public variable on that script by using the name of the variable, and then the name of the variable you are trying to access (variable.variableYouAreTryingToAccess).

Comment
Add comment · 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

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

81 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

Related Questions

show sync var on two text ui (unet - networking) 3 Answers

Printing text one by one, and changing text colors using Markup 0 Answers

Getting A Variable From a Outside Text File 1 Answer

Text popup on target hit 2 Answers

How do I bring variables from two scripts to one then have them print on a canvas text? 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