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? :')
11 Replies
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...
oh thank god
should I just.. assign it as 0 up before start?
Idk, should you?
Should variable
logic
be of type int
?Oh, you're right
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
You have a separate
LogicScript.cs
file with a LogicScript
class and AddScore()
method inside that class?
And you want to call that method?yes
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
thank you so much
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
Ah, understandable
good information yes I thank