#sure
1 messages · Page 1 of 1 (latest)
:D
end goal is to create an accuracy display
ive made it so every succesful hit adds 1 to the hits variable
now im trying to get it to add 1 to the shots variable every time you left click
but every time I get errors
can you show your error
maybe a more elegant way of doing this but this is whats erroring:
where shots is set to 0 at the top of the script and a is set to 1
shots += a;
fml
it happens
So now I need to do shots/hits * 100
and then display it on a ui?
So im guessing I need to do that in a script in the UI?
not good at math but yeah that should do it
how do i get the result to display in the UI?
var result = shots/hits * 100 ;
uitext.text = result.toString();
something like that
I have UI and UI Canvas, so add TextMeshPro as a child?
textmesh pro goes in canvas ui
you can make it it's own gameobject , it just replacement for standard text
do i add a component to TMP?
TMP is a component you add to gameobject
?
yup
and then I input
var result = shots/hits * 100 ;
uitext.text = result.toString();
to which part of TMP?
ideally you would add that to a separate script called UIDisplay or something
then you'd have to pass those values to that script or you can for now add that to where you already have shots/hits variables and just refernce tmp on the top of the script
where it says using Unity.Engine;
add using TMP;
read the docs how to implement TMP tho
The type or namespace name `TMP' Could not be found.
Are you missing a using directive of assembly reference?
will read the implementation thing
Thanks, give me a second to try it
Which part of the script do I put this in? At the end, in the update function?
ideally make a separate function/method, call it only when you shoot / update it.
SetUiText(){ tmp.text = result.tostring();}
also there is a separate component in textmesh pro thats confusing
there is TextMesh and TextMeshUI

yep
ok that means it goes in UI
to call it
public TextMeshProUGUI yourText;
notice the UGUI at the end , as it isn't a normal textmeshpro element (ones that usually can go outside of GUI)
you declare that at the top where you declared your other variables such as "shots"
now the script knows what yourText; is and it is a TextMeshProUGUI;
for my context I could call yourText Accuracy?
yes,
cool
after you save that script you should see a field now in the inspector to plug that Component in, drag and drop that component
or you can hit
should automatically find all compatible tmp components
Where is this button?
did you save the script ?
Yes
and let unity recompile
yup
now where do i put
var result = shots/hits * 100 ;
uitext.text = result.toString();
put it in a method you call everytime u want the text to update
In targetshooter? Ok
yeah because you already declared variables there for now
later on you ideally want to separate all these functions into different scripts so they don't all rely on eachother
is your IDE intellisense working ?
configure your IDE properly
you could avoid these errors
there's a reason the variables are all blue, even INPUT isn't being picked up by Visual Studio
Input should be Green
oh dear
and it's ToString()
that's why intellisense is needed
it's a must if you plan on working on this without headaches, it will literally tell you if something is wrong like spellcheck
lmao wow I wouldn't code anything without intellisense
guess that's what read me are for
my cs teachers are so useless so i dont think they know its a thing either
they just send us 50 page pdfs they pirated from zenva or something
your cs teachers are horrid then wow ...
if you serious about CS material you should try https://www.edx.org/course/introduction-computer-science-harvardx-cs50x
yeah i will start just self teaching tbh
im just trying to finish this group project that I thought would be a walk in the park lmao
it should be a crime to be in cs teaching and not know about intellisense
how do i fix
replace uitext with Accuracy
Ahhhhh
ideally it should be named accuracyTextUI
remember the scope is to confuse yourself less.
well my text looks like this
I suck with making things less confusing
so you can read it back and it makes sense, you can start with **LongVariableNamesLikeThisAreMoreDescriptive **
you have to visually set up TMP properly in ui
its saying new text
so how would I make it say the accuracy?
have i missed something
ah wait
might actually be working
Yeah its working, but its not going down when I miss shots
did u set hits?
Worked before
transform.position = TargetBounds.Instance.GetRandomPosition(); line 9 target. cs
show whole script
using System.Collections.Generic;
using UnityEngine;
public class Target : MonoBehaviour
{
public void Hit()
{
transform.position = TargetBounds.Instance.GetRandomPosition();
}
}
all of target.cs
cool changed nothing and it works now
when I add this it stops working
1Target target = hit.collider.gameObject.GetComponent<Target>(); if(target != null) { target.Hit(); hits +=a; Debug.Log(hits); } if(Input.GetMouseButtonDown(0)) { shots += a; }
Below
if(Input.GetMouseButtonDown(0))
{
var result = shots/hits * 100 ;
Accuracy.text = result.ToString();
}
1Target ?
that was just a typo when pasting
debug your hit.collider make sure its actually hitting where the script is
its so weird it works and then it doesnt
again Debug.Log is your best friend for these
Ok so I found my problem I think
So its meant to count every left click as a shot, but instead its only counting every time the target is destroyed as a shot
fixed that
oh ok
its not working but I think I can fix it
Accuracy.text = result.ToString(); this is correct yeah?
everything works but the maths at the end
if you named the textmeshpro component accuracy then yes
the text updates, but to 0
the hit variable and the shots variable update correctly and can be seen in console
Debug.Log("Accuracy:" +result);
try setting it in update and see what happens
{
var result = hits / shots * 100;
Accuracy.text = result.ToString();
Debug.Log("Accuracy"+result);
}```
like this?
i think its ```cs What math formula can i write to make this?
Percentage = Shots_Hit * 100 / Shots_fired.
Or...
Percentage = Quantity * 100 / Capacity.
found on google
like i said im terrible with mathsss
oh
no the equation would work if it was in excel it might be a order of operations thing in c#
it should be working
❤️
so fucking helpful you have no idea
my pleasure
you too. Hope it works out. you can DM any time you need more help
it can get confusing with the wrong tuts haha
