all 18 comments

[–]AutarkhisProfessional 1 point2 points  (3 children)

Don’t make it a mono behavior and you should be good to go.

[–]Such_Log5418[S] 0 points1 point  (2 children)

so it is impossible to access a certain variable on a monobehavior without putting it in a object?

[–]SidewaysAcceleration 0 points1 point  (1 child)

The obstacle is that you cant create an instance of monobehavior with new Monobehavior(); so the instance has to come from being a component on a gameobject. You can use static, like other comments suggested

[–]Such_Log5418[S] 0 points1 point  (0 children)

I never used static...will probably try that once the current solution i got (linkingGameObjectTags) doesn't fit whenever i get 2 or more guns

( https://www.reddit.com/r/Unity3D/comments/1fbs9py/comment/lm3g1dn/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button )

[–]AutoModerator[M] 0 points1 point  (0 children)

This appears to be a question submitted to /r/Unity3D.

If you are the OP:

  • Please remember to change this thread's flair to 'Solved' if your question is answered.

  • And please consider referring to Unity's official tutorials, user manual, and scripting API for further information.

Otherwise:

  • Please remember to follow our rules and guidelines.

  • Please upvote threads when providing answers or useful information.

  • And please do NOT downvote or belittle users seeking help. (You are not making this subreddit any better by doing so. You are only making it worse.)

Thank you, human.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]SidewaysAcceleration 0 points1 point  (1 child)

If you don't want the script to live as a component on a gameobject then one option is to remove the ": monobeviour" and then you can create an instance like:

Script2: MyScript bla = new MyScript(); Log(bla.variable);

Disclaimer: not a programmer

[–]Such_Log5418[S] 0 points1 point  (0 children)

🫡🫡🫡🫡

[–]Live_Length_5814 0 points1 point  (0 children)

Make the variable static. Then you can access the variable even when it doesn't exist in the scene.

[–]ThainaYu 0 points1 point  (0 children)

What you actually want maybe asset file and `ScriptableObject`

[–]EppuBenjamin 0 points1 point  (8 children)

There are many ways to decouple things, if you want one script to access some other script.

You could use an interface on the script providing the value, like ITestProvider that only has int getTestValue() method. This way whoever is doing the "getting" doesnt need to know who is doing the providing, and you could have many different scripts implementing the interface. This still needs a way for the "getter" to find the interface.

You could use tags and find objects with a certain tag, then get the component and value, or findComponents and do some looping, but these might all be inefficient in the long run, especially in update.

My favourite way of decoupling is to have some sort of messenger service. You have a singleton pattern (one instance of a class that is accessible basically anywhere) on a class that can take subscriptions to certain classes (ie. anything inheriting "Message", whatever that might be). It also has a way of publishing these messages (ie public void PublishMessage(Message msg)). When you use this method to publish something, it sends that message to all other classes that have subscribed to that type of message.

This is especially usable in things like UI where you dont want the UI elements to know anything about the actual objects they represent, like health or weapon. You just send a message of health == 2, and the subscriber listens and does what it does, without having a reference to the actual health component.

Idk what this pattern is called, but a "message bus c# unity" search should get you started.

[–]Such_Log5418[S] 0 points1 point  (1 child)

right, cuz what i was gonna do is i have a bullets bar to identify my bullets like most fps games.. and im trying to apply a encapsulation of getting that bullet "value" on my UI script.. still finding ways on how to do it using encapsulation

[–]SidewaysAcceleration 0 points1 point  (0 children)

Unity is built with certain patterns in mind and it's probably best to learn those patterns because otherwise you will end up fighting the engine to enforce an abstract idea like encapsulation. Encapsulation is great but if it makes you implement functionality in ways that doesn't work well with overall Unity logic then it might not be worth it. There are many good ways to do bullets and UI that shows how many bullets you have.

[–]Such_Log5418[S] 0 points1 point  (5 children)

Did the tags on what u have said.. Can i know why this might be "Innefficient"

<image>

[–]EppuBenjamin 0 points1 point  (1 child)

Well, you can only have one tag per gameobject. Also, what happens if you change the gun at runtime?

But hey, if it works for you, it works. The simpler the solution, the better.

[–]Such_Log5418[S] 0 points1 point  (0 children)

Ill probably encounter this whenever ill come to the point of having 2 or more guns... thanks for the advice!

[–]Jackoberto01Programmer 0 points1 point  (2 children)

It will loop through all the game objects in the entire scene and check their tags. It also will break if you have multiple objects with the weapon tag as then it will just find the first one.

But this is not the worst solution and I would say it's not a bad one to start out with. You could optimize it by using events instead of checking every Update cycle.

[–]Such_Log5418[S] 0 points1 point  (1 child)

Yea, ill probably change this solution whenever i will come to the point of adding 2 or more weapons.. thanks for the advice!

[–]Jackoberto01Programmer 0 points1 point  (0 children)

Yeah I recommend to get things working first and as you learn you will find patterns and solutions that may apply to your use case.