I keep bumping on this problem on several of my projects and now I would love to know how to handle this.
I have a script attached to a prefab (a shelf). In the script I use AddListener so I can use buttons from the UI to start a method.
The problem is, every time I use the button, it will fire as many times as I have prefabs of my shelf in the scene.
So if I have 1 shelf, the button will be called once, if I have 5 shelves it will be called 5 times.
Added to this it doesn’t matter if the shelves are active or not in the hierarchy, the method is still being called.
I have read about instances but I can’t wrap my head around it.
If someone would be so kind and explain (at best with script examples) how I can talk to only 1 Shelf at a time. Thanks guys.
Each of your shelves are an instance of the class. It sounds like you are subscribing them all to the same event so they are all listening to it.
What are you wanting to do? What happens when you click the button and could you not just have it so they have to click on the shelf rather than a button? Either that or you need a way to select which shelf you want the button to affect when and then point it to that instance.
I don’t think the forums would like me to post 500 lines of code per script
In short:
List<Games> games = new List<Games>();
button.onClick.AddListener(delegate { AddGameToShelf(0); });
void AddGameToShelf(int type)
{
games.Add(type)
}
in fact I have 1 UI button adding 1 to the List.
This button is subscribing to the script, so if I have multiple instances of the shelf, the button is subscribing multiple times. which in turn is like clicking several times on the button.
as said, this is the simple version. in fact I have 10 buttons in the UI adding different types of games to the shelf. So clicking on the shelf wouldn’t help, I need more options which is why I went for buttons.
in other words I would need as many UI panels as I have shelves, which can’t be the option.
There must be a way to talk to a single instance of a specific shelf using only 1 UI Panel.
I guess it’s nothing else than chests using 1 UI but having their own inventory.
Maybe you can open the UI with a method that sends itself (meaning the shelf) as a parameter. Then, you can call your method with a value from the shelf’s script. If you can select a new shelf while the UI is open, just update which shelf is ‘active/selected’ then.
Maybe. I just don’t know how to achieve this. My Short term memory is “broken”, so whenever I program, I tend to forget how I did things. I programmed inventories and chests in the past and it worked, but I can’t remember how, if that makes any sense.
I’m using the OnMouseDown, could I put the AddListener on this and maybe disable it when I close the UI? Would the script know it’s supposed to only work for this instance?
It’s partly why people get hung up on how code should be presented and use design patterns, because that dramatically reduces the amount you need to remember. You’re not special (or alone).
Does get easier if you’re doing programming full time as obviously, you will remember more, but the problem is quite normal. Good programming practises, perfect indentation, self discipline and minimal comments, all create maintainable code, and maintainable code is about not just you remembering what you were thinking but allowing other programmers to follow the train of thought.
So I think your broken short term memory seems remarkably common. I would say that’s most people here, so maybe look at imposing a few more rules on your own code.
I wish you were right and it would only be because of code maintenance, but it’s probably part of the problem
I had a heart issue some time ago and my short-term memory got affected. It’s not just programming.
I forget to eat, I sometimes even forget I ever talked to someone, could be 10 minutes ago or 10 days.
Sometimes I ask the same question several times, I just forget I asked.
It’s nothing really bad, just have to live with it is all.
When programming, it’s really annoying because I know I already made something similar but it won’t come back.
2 days ago I used DateTime and could not figure out how to make a simple comparison of 2 dates, I rested for an hour and realized it was just a simple date2 - data1. It’s just frustrating.
Coding helps me train my brain, so it’s a good thing