U
U
Uncle Bogdan2021-06-24 19:04:33
C++ / C#
Uncle Bogdan, 2021-06-24 19:04:33

Why is a new element not being added to the collection?

Tipo there is a collection, but elements are not added to it.
Gives an error message:

NullReferenceException: Object reference not set to an instance of an object
BoxController.OpenBox(System.Int32 BoxId) (at Assets/Scripts/BoxController.cs:102)


The code:
Brawler PPBrawler = playerStats.brawlers[Random.Range(0, playerStats.brawlers.Count)];
print(PPBrawler.Name); // PPBrawler не равен null
int count = Random.Range(Boxes[BoxId].PPAmount.x, Boxes[BoxId].PPAmount.y);
items.powerPoints.points.Add(count, PPBrawler);
PPBrawler.PowerPoints += count;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kypcant, 2021-06-24
@motkot

If items is list <T>, then, as far as I remember, only the element that you want to add to the list should be passed as an argument to the Add method, or there is an overload that I don’t know about.

V
Vitaly Kachan, 2021-06-25
@MANAB

What exactly is in line 102 of BoxController.cs? Because the error says that you are accessing an uninitialized variable (to NULL). In this case, without specifying the line numbers, it is not clear whether this can happen in a different class / line at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question