Using code to reference code

So I have a collider, and I have code elsewhere that tells my UI counter to go up. That code lives in LogicScript and it's called addScore. I'm trying my best to reference it within my collider's script using the tag "Logic", but there's definitely something wrong with that big logic = line as well as the logic.addscore line. Does anyone have any advice? :')
No description
11 Replies
Angius
Angius4mo ago
Well, the variable logic has not been declared anywhere here So you can neither give it a value like you're trying to do in Start() Nor can you access its members like you're trying to do in OnTrigger...
Mister8luesky
Mister8lueskyOP4mo ago
oh thank god should I just.. assign it as 0 up before start?
Angius
Angius4mo ago
Idk, should you? Should variable logic be of type int?
Mister8luesky
Mister8lueskyOP4mo ago
Oh, you're right
Mister8luesky
Mister8lueskyOP4mo ago
I lack so much coding vocabulary and it is apparent I'm mostly just not sure how to tell the code to run the addScore void from the LogicScript code. I know this is wrong, aggghhh
No description
Angius
Angius4mo ago
You have a separate LogicScript.cs file with a LogicScript class and AddScore() method inside that class? And you want to call that method?
Mister8luesky
Mister8lueskyOP4mo ago
yes
Angius
Angius4mo ago
Well, then, how you do that depends on whether the method is static or not If it's static, then just LogicScript.AddScore() If it's not, you'll need an instance of LogicScript
var script = new LogicScript();
script.AddScore();
var script = new LogicScript();
script.AddScore();
Mister8luesky
Mister8lueskyOP4mo ago
thank you so much
Buddy
Buddy4mo ago
Note that in Unity you cannot initialize a new instance of a class derived from Monobehavior You must use AddComponent<T>() or GetComponent<T>() if the component already exists on the entity And please, avoid this. It will become cluttered real fast By this I mean FindGameObjectWithTag(..).GetComponent<T>() Either use a static class for score keeping or use a game manager script with a Singleton
Mister8luesky
Mister8lueskyOP4mo ago
Ah, understandable good information yes I thank
Want results from more Discord servers?
Add your server