- Home /
How to make 3D texts show ammo?
Allright ,so I got my script for shooting ,and now I want when my ammo decrease in the script to make 3d text show that on screen in game mode ,I will have to make a few more variables for that but how do I conect those two to interact with each other.
I gues I will need two 3d texts , one for current ammount of bullets and one for number of clips.
This is not a GUI text so function OnGUI wont work here.
I need some hints ,its basicaly just to connect them to work together.
If you need more information from me ,if this question is not precise ,say it ,This is really important for me.
Please.
So far I tryed adding first variables into my shooting script:
var ShowCurrentAmmo : Transform;
var ShowClipsLeft : Transform;
So I can insert the 3d text mesh into the script in inspector but other than that I am lost ...
I posted an answer about 20 $$anonymous$$utes ago, still waiting for approval.
Thanks $$anonymous$$ali ,I wonder why the answer is waiting for approval?
Answer by Amirmy · Aug 18, 2014 at 07:29 PM
You Should Use textmesh Not Transform
You can Do it like (C#):
Public TextMesh ShowCurrentAmmo,ShowClipsLeft;
void UpdateAmmo()
{
ShowCurrentAmmo.text = AmmoValue.ToString();
ShowClipsLeft.text = ClipValue.ToString();
}
Then call UpdateAmmo(); when the player shoots. or just put ShowCurrentAmmo.text = AmmoValue.ToString(); and ShowClipsLeft.text = ClipValue.ToString(); after the ammo decreases.
Also don't forget to Drag and drop your 3dtexts in ShowClipsLeft and ShowCurrentAmmo.
Javascript:
var ShowCurrentAmmo: TextMesh;
var ShowClipsLeft: TextMesh;
ShowCurrentAmmo.text = AmmoValue.ToString();
ShowClipsLeft.text = ClipValue.ToString();
ps: I would recommend to use Gui Text for better experience.
Thank you so much ,you are the best ,I would buy you a drink if you were close to where I live :D
@Grizzly , You're most welcome! I'm more than happy to help you out, cheers!
Answer by KaliTech · Aug 18, 2014 at 07:29 PM
Hi,
You don't actually need two 3d texts, you could use one that, as an example will say 10/2, where the 10 is the amount of bullets, and 2 is the amount of clips you have.
What you need to do is take a TextMesh as a variable, and in your Update function you could just set the text with: yourTextMesh.text = bullets.ToString() + "/" + spareClips.ToString();
(Untested code)
Just change the variables accordingly.
Best of luck,
KaliTech.
Answer by DESTRUKTORR · Aug 18, 2014 at 06:30 PM
You should only need one 3D text. You will just need to use two variables to set its text value.
Create a 3D Text object (either by adding the TextMesh and TextRenderer components to a GameObject, or via the GameObject/Create menu command)
Get a reference to the TextMesh component for your 3D text, in the script that handles the shooting.
Convert the two variables into strings, and set TextMesh.text to the string concatenated between the two, assuming you want just a slash between them.
If you want to do something else, then you may want to have two separate TextMesh objects, yes. However, managing two references would be extremely similar to managing one reference, just you would need to set the "text" attribute separately for both objects, and to the appropriate values.
First of all ,thank you very much for this information I cant find anything about this on google ,but the thing is I never worked with this before so I dont really know how to pull this off ,can you explain more please ,like how the code lines should look ,or if you need ,I might even add my script here ,I doubt it needs lot of lines to make it work.Sorry if I sound like jerk now ...
If you need to ask how the syntax looks, you may want to consider looking more into how to program in C# (or just about any C-based language, for that matter) than how to program for Unity.
Answer by $$anonymous$$ · Aug 18, 2014 at 07:26 PM
In C#, it would be like this.
TextMesh.text = AmmoLeft.ToString() + " / " + AmmoLeftInClip.ToString ();
Im not very good in Javascript, so you may end up having to change a few things in this code in order to get it to work. In C#, this should work.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Trouble with raycasting 0 Answers
My timer script makes Unity crash... 0 Answers
Gun Script Help 2 Answers