Referencing GameObjects, Scripts, variables, fields, methods (anything non-static) in other script instances or GameObjects:
It isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.
Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.
That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.
If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.
“Stop playing ‘Where’s GameWaldo’ and drag it in already!”
More information is needed. Where is “Slot4” with respect to this particular component and the game object it’s on? Child game object? Same scene? Different scene? Does it relate to this EquippingScript?
You’ll only get general advice like Kurt’s without being more specific.
I have seen you say all that before in other things when I looked for a way to do it. I am not using find but I don’t know how to only do it for a GameObject and I do not need to know for variables.
If you already have a reference to EquppingScript, then it’s just script.Slot4, though these are not very good script/variables names.
You should also look to learn how to use collections such as arrays and List. Both accessing fields and using collections are basic C# things, so it’d be worth finding some courses on basic C# to get up to speed.
Well, now it is giving me an error on true. Cannot Implicitly convert type bool to Unity.Engine.GameObject, any idea why? I have done a bit of C# but have not done much with collections.
I have done some, they just didn’t have anything on that, this is a learning experience for me. Still, what should I do for this part as true is giving me an error?
Definitely take a moment to review your C# language famliarity… no sense in you confidently telling the compiler absolutely gibberish because it will absolutely do whatever gibberish you can get it to swallow. Mixing up = for == is one such example and there are many more.
The only defense is to become confident in antagonistically reading the code you wrote. Read it as if it was written by someone else and you have to fresh-understand it. I get into most of my trouble by failing to read an obvious typo I just wrote 30 seconds ago, squirreling around until I corral it down to the simplest test and attach the debugger and say “Wait a second, this value just BECAME true after I tested it, how is that possible?!”
Debug, debug, debug.
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.