- Home /
 
Ammo Script
Hi I'm making an ammo script and I have been looking through other questions but I cant really seem to find my problem in them. When I call my "reserveAmmo" I am receiving an error: Cannot implicitly convert type 'string ' to 'UnityEngine.UI.Text'. I have been using ToString() on other parts of my code but for some reason it does not seem to be working... const int capacity = 10; int currentClip; int reserveAmmo = 20; // GUI Ammo public Text clipText; public Text ReserveAmmoText; public Text ReloadText;
 void Start ()
     {
        clipText.text = "Clip : " + capacity.ToString();
         ReserveAmmoText = "Reserve Ammo : " + reserveAmmo.ToString(); //error is here
     }
 
 void Update () {
     if (gun.active)
         {
             if (Input.GetButtonDown("Fire1"))
             {
                                            int x = 110; for (int i = 0; i < currentClip; i++) ;
 
                 x += 8;
                 if (currentClip > 0)
                 {
 
                         currentClip--;
                 }
                 else
             {
                 if (reserveAmmo > 0)
                 {
                     if (Input.GetButtonDown("R"))
                     {
 
                         currentClip = capacity;
                         reserveAmmo -= capacity;
                     }
                 }
             }
 
             }
         }
         
 
     }
 
               I have cut out a bunch of code that I don't think is necessary so there might be the odd missing or too many bracket. Thanks for help if anyone can help me!
What is the type of ReserveAmmoText ? If it's a UIText you may need to add .text at the end: ReserveAmmoText.text = "Reserve Ammo : " + reserveAmmo.ToString();
Answer by Cherno · Nov 17, 2015 at 03:08 PM
You have to access the Text component's text variable, not the component itself (as you already did correctly in the line before, with clipText.text):
 ReserveAmmoText.text = "Reserve Ammo : " + reserveAmmo.ToString(); 
 
              Your answer
 
             Follow this Question
Related Questions
[Networking] Changed text does not appear changed for players who just connected 0 Answers
How do I bring variables from two scripts to one then have them print on a canvas text? 0 Answers
How to display health on screen as text? 1 Answer
How to make a Score GUI. 0 Answers
Text being part of a function? 1 Answer