Proper way to Call a Method

Hi I’m fairly new to code games and in my newest project I’m attempting to make a super simple firing range. The range is setup where if the target is hit it’ll fall over. Right now I’m working on a retry button that resets the targets back to the original standing position. I’m assuming I can just take the original rotation of the targets and call a method the reverts the fallen target through an If statement to pop them back up. My question is which is the more ideal place to implement the method. Right now I have two place that could potentially work. First one is in the button script where right now all its has working is when the user presses “e” the button moves into the wall and eventually back outwards. OR should I put the method into to the Target script itself and then call said method through the button script when the user presses “e”. Sorry if I didn’t word this the best, my terminology is very limited so I’m happy to clarify things if needed. I will attach the two scripts in question. Please keep in mind lots of the code isn’t the cleanest but at this stage I just want things to kind of work for now and I will try to clean my code once the main pieces are working.

6579613–746950–RetryBtn.cs (1.59 KB)

If you don’t mind everything in the scene resetting, a common way to do this is just reload the scene and start afresh.

If you need finer-grained resurrection, one way is to make “resurrector” scripts to watch each of your targets, and then when the scene starts, they do this:

  1. disable the actual target
  2. clone the disabled target
  3. move the clone to the correct position
  4. enable the cloned target
  5. now sit in a loop waiting for whatever condition it is to reload
  6. when it is time to reload:
    — destroy the cloned knocked-down target
    — make a fresh copy off the original disabled one
    — position it (as above)
    — enable the clone

Lather rinse repeat.

1 Like

Suppose the ball can Fall() when then player says to, or a wall can make it Fall(0 on a collision, and maybe some other ways. Then it makes sense to put it on the ball’s script like you have. Anyone can call theBall.Fall(). But suppose Fall() depends on some special variables in the player, like power-ups to increase fall speed. Then it makes sense to have it in the player.

If neither of those apply, it makes sense to guess whether one will. Putting it with the ball is the default “this is probably where I’ll want it if it ever matters” location.

1 Like

Thank you for the help! I understand what you mean and that is definitely going to be helpful to me when I have multiple moving variations of targets. Is there by chance a easier method I could use for the time being? Mainly because I’ve not setup multiple scenes yet and kind of like how you said I really don’t want to reload the scene every time my player interacts with the retry button. Since right now the targets are stationary I figured I could do something very similar to my Fall() function located in the Target script but instead of using the Targetfall Vector. I Would type something like " transform.localEulerAngles = orignalTargetPosition;". I mean of course I’m not 100% sure that will work but something along that line of just a simple rotation back to the upwards position when the button is pressed. My main problem is I get often confused on how to call a function depending on where they are located. To describe my problem a little bit better lets say I have a method called “ResetTargets()” that works how I imagine and it simply rotates the fallen targets. My confusion is, where is the more ideal place to put that said working method. Both scripts have some pros and cons. My Target script which is only applied to the targets themselves provided a more consistent code since any future Gameobject I create can be given access to my Target class and all its methods. The problem with that is I’m not fully sure the process of how Unity refences other classes so I don’t know if I would have access to all the things I need. And for putting the “ResetTargets()” method into my button script I believe it would provided a quick and easy way of calling it. Looking into the future though I’m wondering if that’s a very sloppy way of using methods mainly because if I wanted make anything else that would rotate the targets like more buttons or a pressure plate, I would have to reuse the same code in any new scripts. Should this processes be generally avoided when making games? Sorry for the long question I just want to make sure I understand good habits when it comes to interacting with other game objects scripts.

Here is one guiding principle:

https://en.wikipedia.org/wiki/Separation_of_concerns

I don’t adhere to it fully, but I do try to group stuff so that it takes care of one thing, then make clear boundaries between “things”

For instance, I would make a target script with the following entrypoints:

Fall
AreYouFallen ?
Restore

Then anyone wanting to talk to that target would do so with the above methods.

Later, if some targets had more points than others, I could add a method that returns how much they are worth.

1 Like