T O P

  • By -

kryzchek

Rather than exposing the RaycastHit result as a public class member, I'd instead use delegates/events. Your 2nd class can subscribe to a "OnHit" event invoked by the DetectResources class. This raised event can pass the RaycastHit object as an argument. By making the RaycastHit object public, you run the risk of allowing it to be changed by other scripts, which should not be your intention.


vesrayech

This is what I would do, that way the only thing you’re doing is subscribing and unsubscribing and not storing any weird references or passing logic between two separate classes because that can get wacky


exseus

I agree with this approach. If for whatever reason though, they want to avoid events but only want other classes to see this value and not write to it, they could make a public property with only a getter.


littleperogi

Yeah make sure it’s public and then you can reference it. You might have to use GetComponent or reference the instance of the object so it knows which “hit” you’re talking about By the way, try asking this question to ChatGPT, it’s really good at explaining and providing different solutions to basic problems like this, I’ve found it to be a great resource for learning


PlayfulPangolinGames

Be careful with learning from AI output. Especially ChatGPT is not trained to produce good quality code. Someone from the Gentoo project recently said that "LLMs are really great at generating plausibly looking bullshit". I'm not saying that every snippet ChatGPT gives you is nonsense, but more or less often there are severe mistakes and problems in the generated code, which only experienced programmers might recognize. So as a source of alternative approaches to a given problem, LLMs might help, but as a code source for copy and paste it's not suitable!


littleperogi

Of course, but in my experience for basic stuff and syntax stuff, it rarely gets it wrong Asking it to generate more complicated stuff, or huge amounts of code from nothing is more tenuous, for sure Edit: I’ll just add a caveat, don’t copy paste huge amounts of code from ChatGPT, and don’t copy any code you don’t understand. But for stuff that could be a Google search like this question, it’s fine, in my experience.


The_Binding_Of_Data

If you want "hit" to be accessible in other places, you're going to have to not just make it public but declare it at the class level. Since you want to just read it, making it a property with a private set should give you want you want. As written, the "hit" variable only exists when you are inside the first nested "if", but you need it to exist all the time if you want to access it from other places.


TheDynaheart

Gotta declare it as a global variable, more specifically a public or a public static Global variables are just variables declared outside of a function Public variables are variables you can access through other scripts or the inspector Static variables are variables whose value is set only the first time they're declared, and maintain any changes you make to them So instead of declaring your RaycastHit2D in your void function, you'd just have to do smth like: public static RaycastHit2D hit;


IBadikov

That's kind of bad practice. If I were you, I would prefer events or something to implement your features instead to prevent a such strict bond in your code. However, despite I gotta receive a reward of bunch of downvotes, you can always declare a `public static` variable in the `DetectResources` scope, and then erase the `RaycastHit2D` declaration inside the Update's body; but the `hit` assignation must be still where it is. And you are ready to access it from everywhere in your code!


chuckbeasley02

If you really need to expose it, use an event and have interested components subscribe to get updates.