#sure

1 messages · Page 1 of 1 (latest)

tulip charm
onyx wave
#

: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

tulip charm
#

can you show your error

onyx wave
#

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

tulip charm
#

shots += a;

onyx wave
#

fml

tulip charm
#

it happens

onyx wave
#

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?

tulip charm
#

not good at math but yeah that should do it

onyx wave
#

how do i get the result to display in the UI?

tulip charm
#

var result = shots/hits * 100 ;
uitext.text = result.toString();

#

something like that

onyx wave
#

so add a script to the UI text ?

#

first time coding in unity Smile

tulip charm
#

make a UI manager and do all the code for the UI there

#

use TextMeshPro

onyx wave
#

I have UI and UI Canvas, so add TextMeshPro as a child?

tulip charm
#

textmesh pro goes in canvas ui

#

you can make it it's own gameobject , it just replacement for standard text

onyx wave
#

do i add a component to TMP?

tulip charm
#

TMP is a component you add to gameobject

onyx wave
tulip charm
#

yup

onyx wave
#

and then I input

var result = shots/hits * 100 ;
uitext.text = result.toString();
#

to which part of TMP?

tulip charm
#

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

onyx wave
#

The type or namespace name `TMP' Could not be found.
Are you missing a using directive of assembly reference?

#

will read the implementation thing

tulip charm
#

sorry it was using TMPro;

#

top of my head haven't used TMP in a while lol

onyx wave
#

Thanks, give me a second to try it

onyx wave
tulip charm
#

ideally make a separate function/method, call it only when you shoot / update it.

#

SetUiText(){ tmp.text = result.tostring();}

onyx wave
#

this shit is so confusing

#

give me a minute

tulip charm
#

also there is a separate component in textmesh pro thats confusing

#

there is TextMesh and TextMeshUI

onyx wave
tulip charm
#

ok look

#

when you add textmesh component

#

see how it's Text (UI) ?

onyx wave
#

yep

tulip charm
#

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;

onyx wave
#

for my context I could call yourText Accuracy?

tulip charm
#

yes,

onyx wave
#

cool

tulip charm
#

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

onyx wave
onyx wave
tulip charm
#

you should see it in the targetshooter script in the inspector

#

next to Accuracy

onyx wave
tulip charm
#

did you save the script ?

onyx wave
#

Yes

tulip charm
#

and let unity recompile

onyx wave
#

oh

#

fixed it

tulip charm
#

show your line 18,30

#

ok good

onyx wave
tulip charm
#

yup

onyx wave
#

now where do i put
var result = shots/hits * 100 ;
uitext.text = result.toString();

tulip charm
#

put it in a method you call everytime u want the text to update

onyx wave
#

In targetshooter? Ok

tulip charm
#

yeah because you already declared variables there for now

onyx wave
tulip charm
#

later on you ideally want to separate all these functions into different scripts so they don't all rely on eachother

onyx wave
tulip charm
#

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

onyx wave
#

oh dear

tulip charm
#

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

onyx wave
#

I had no idea vcs even had that type of integration

#

I only code in python lol

tulip charm
#

lmao wow I wouldn't code anything without intellisense

#

guess that's what read me are for

onyx wave
#

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

tulip charm
#

your cs teachers are horrid then wow ...

onyx wave
#

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

tulip charm
#

it should be a crime to be in cs teaching and not know about intellisense

onyx wave
#

how do i fix

tulip charm
onyx wave
#

Ahhhhh

tulip charm
#

ideally it should be named accuracyTextUI

#

remember the scope is to confuse yourself less.

onyx wave
#

well my text looks like this

onyx wave
tulip charm
#

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

onyx wave
#

oh right

#

should probs watch a yt vid on that

tulip charm
#

go to the scene view and adjust tmp in 2D view

#

yup, it's simple drag and drop stuff

onyx wave
#

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

tulip charm
#

wdym

#

you havent set it anywhere

#

to go down

onyx wave
#

i mean like

#

on a number line

#

its just in general very unhappy now

tulip charm
#

did u set hits?

onyx wave
#

its now erroring in a script that hasnt been involved at all before

tulip charm
#

check line 9

#

you haven't set something

onyx wave
#

Worked before

#

transform.position = TargetBounds.Instance.GetRandomPosition(); line 9 target. cs

tulip charm
#

show whole script

onyx wave
#
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();
                }
tulip charm
#

1Target ?

onyx wave
#

that was just a typo when pasting

tulip charm
#

debug your hit.collider make sure its actually hitting where the script is

onyx wave
#

its so weird it works and then it doesnt

tulip charm
#

again Debug.Log is your best friend for these

onyx wave
#

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

tulip charm
#

oh ok

onyx wave
#

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

tulip charm
#

if you named the textmeshpro component accuracy then yes

onyx wave
#

the text updates, but to 0

#

the hit variable and the shots variable update correctly and can be seen in console

tulip charm
#

make a debug.log for each element and see what numbers uu are getting

#

oh ok

onyx wave
#

but accuracy is showing

tulip charm
#

Debug.Log("Accuracy:" +result);

onyx wave
#

its math erroring

#

but it shouldnt be

tulip charm
#

try setting it in update and see what happens

onyx wave
#
                {
                var result = hits / shots * 100;
                Accuracy.text = result.ToString();
                Debug.Log("Accuracy"+result);
                }```
#

like this?

tulip charm
#

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

onyx wave
#

oh

#

no the equation would work if it was in excel it might be a order of operations thing in c#

tulip charm
#

it should be working

onyx wave
#

OH MY GOD

#

IT WORKS

#

Thank you for your help

#

<3

tulip charm
#

❤️

onyx wave
#

so fucking helpful you have no idea

tulip charm
#

my pleasure

onyx wave
#

have a great day

#

sorry to take up so much of your time

tulip charm
#

you too. Hope it works out. you can DM any time you need more help

onyx wave
#

blame my cs teachers

#

thanks man

tulip charm
#

it can get confusing with the wrong tuts haha