Home > Blockchain >  Removing and re-adding same sprite to list Unity
Removing and re-adding same sprite to list Unity

Time:01-15

I have a Unity script in c# where a public list of sprites is declared and then populated manually in the inspector.

The list consists of 19 sprites of cars.

These cars are available only if the player completed each level.

For example: When you open the game for the first time, you only have 1 available car but if you complete level 1, you unlock another car, when you complete level 2, you get another and so on.

My question is, being that I manually populated the list, how do I go about re-adding the same sprite back when a level is completed?

I was going to use cars.RemoveRange(1, 18) to remove them from the list but do not know a way to add them back without calling every sprite back manually. I believe there is a simpler, better way that I do not know about.

This is the script:

public class CarSelection : MonoBehaviour
{
public List<Sprite> cars; //store all your images in here at design time
public Image displayImage; //The current image thats visible
public Button nextCar; //Button to view next image
public Button previousCar; //Button to view previous image
private int i = 0; //Will control where in the array you are

void OnEnable()
{
    //Register Button Events
    nextCar.onClick.AddListener(() => NextCarButton());
    previousCar.onClick.AddListener(() => PreviousCarButton());
}
private void Start()
{
    cars.RemoveRange(1, 18);

    if (FinishLineTouch.levelOneComplete == true) {
        //Re-adding the same sprites here
    }
}
public void NextCarButton()
{
    if (i   1 < cars.Count)
    {
        i  ;
    }
}
public void PreviousCarButton()
{
    if (i - 1 >= 0)
    {
        i--;
    }
}
 void Update()
{
    if (LevelSelect.isCarChosen == true) {
        nextCar.interactable = false;
        previousCar.interactable = false;
    }

    displayImage.sprite = cars[i];
}
}

Thanks in advance.

CodePudding user response:

You have a small flaw in your logic here. Saying that you add the car sprites in the inspector, removing all the sprites is not ideal.

You can have a list of all the sprites, and a list with available options (starts with sprite[0] and adds each time you complete the level).

However, if you want to know how to add them back after deleting them, you need to make a copy in a global variable, so it's more efficient to try what I am suggesting above.

CodePudding user response:

You should have two different arrays, one for the sprites, and the other one for if the player unlocked it yet. Or you should create a custom class that has the sprite of the car, and if it is unlocked yet.

CodePudding user response:

Sounds like all you really need to do is make sure the index doesn't exceed the unlocked cars

if(i   1 < cars.Count && i   1 <= amountOfFinishedLevels)
{
    i  ;
    // and then you really should do this in both methods and not every frame
    displayImage.sprite = cars[i];
}  

and don't remove/readd any items at all.

  •  Tags:  
  • Related