Want to destroy mini map icon when the boost is picked up in game

I have a minimap in the game which shows the boost pickups in the real game. I want to destory their icons when the actual pickup is picked up.
Now ive made a while loop in my function GenerateIcons() which goes like this

while (true){
            if (boost_real == null){
                      Destroy(boost_minimap);
                      break;}
}

Except my code just never compiles when I do this. The unity editor just shows executing code for eternity

1 Like

Hi,

Your code looks fine to me albeit hard to read due to the formatting. What compiler error do you get? Does it refer to your while-loop?

1 Like

Yes. while (true) means ‘execute forever’. It will only exit that loop if boost_real becomes null, but this will continue ‘forever’ without passing the execution back to Unity, so Unity can never complete the frame. I suspect you are trying to ‘monitor’ the boost. Without knowing what your code looks like, it’s hard to suggest a way to fix it

1 Like

No error. The game window just doesnt open. It loads forever

1 Like

boost_real becomes null when the player picks it up. How else should I do it? I’ll attach my code

In MiniMap script

public GameObject miniMap; //Canvas object
float miniMapRatio; //Ive calculated this already at start

public void GenerateMiniMapBoost(Vector3 boost_wm, int boostType, GameObject boost){
        Image newImage = Instantiate(boost_mm[boostType]);
        newImage.transform.SetParent(miniMap.transform,false);
       //Finding the icon location in minimap. Works fine
        newImage.rectTransform.anchoredPosition = ref1_mm.anchoredPosition +
                                                    new Vector2((boost_wm.x - ref1_wm.position.x)*miniMapRatio,
                                                                (boost_wm.y - ref1_wm.position.y)*miniMapRatio);
        
        Destroy(newImage,15); //Automatically destroys the pickup and the icon in 15 sec if user doesnt 
        //pick it up
       
        while (true){
            if (boost == null){
                Destroy(newImage);
                break;

In Pickup script

[SerializeField] GameObject[] boosts; 

    [SerializeField]GameObject[] roads;
    public MiniMap miniMap;

    void GenerateBoost(){
        
        int randomNumObject = Random.Range(0,boosts.Length);
        int randomNumRoad = Random.Range(0,roads.Length);
        Vector3 loc = roads[randomNumRoad].transform.position;
        GameObject boost = Instantiate(boosts[randomNumObject], loc, Quaternion.identity) as GameObject;
        miniMap.GenerateMiniMapBoost(loc,randomNumObject,boost);
        Destroy(boost,15);
        
    
    }

Ive tried to include only the relevant parts in the above scripts

1 Like

Yes, that will not work. Like I said, that loop never returns control back to Unity so the frame never finishes.

Your boost and minimap icon is disconnected from each other. Why don’t you just let the boost remove the icon?

Let the minimap return the image, and then the boost can destroy it when the 15 seconds expire or the player picks it up

// changed to return the image
public Image GenerateMiniMapBoost(Vector3 boost_wm, int boostType, GameObject boost)
{
    Image newImage = Instantiate(boost_mm[boostType]);
    newImage.transform.SetParent(miniMap.transform,false);
    //Finding the icon location in minimap. Works fine
    newImage.rectTransform.anchoredPosition = ref1_mm.anchoredPosition +
                                                    new Vector2((boost_wm.x - ref1_wm.position.x)*miniMapRatio,
                                                                (boost_wm.y - ref1_wm.position.y)*miniMapRatio);
    return newImage;
}

// Pickup script
GameObject boostObject;
Image boostImage;

// Call this when the boost is picked up by the player
void DestroyBoost()
{
    Destroy(boostObject);
    Destroy(boostImage);
}

void GenerateBoost()
{   
    int randomNumObject = Random.Range(0,boosts.Length);
    int randomNumRoad = Random.Range(0,roads.Length);
    Vector3 loc = roads[randomNumRoad].transform.position;
    boostObject = Instantiate(boosts[randomNumObject], loc, Quaternion.identity) as GameObject;
    boostImage = miniMap.GenerateMiniMapBoost(loc,randomNumObject,boost);
    Invoke(nameof(DestroyBoost),15);
}

The other option would be to add an event on the boost and let the minimap listen for the event. You will have to be able to associate boosts to their respective images and when the boost is picked up or destroyed, you’d fire the event and the minimap can clean up the image

1 Like

There are multiple boosts on the map at the same time. Thats why I created the variable inside the func. Will the first soln still work?

1 Like

No. It assumes there’s a Pickup for each boost.

Create a script called Boost.cs and put it on each boost.

public class Boost : MonoBehaviour
{
    private Image minimapImage;

    public void SetMinimapIcon(Image image)
    {
        minimapImage = image;
    }

    public void OnDestroy()
    {
        Destroy(minimapImage);
    }
}

Then, update the Pickup script

// Pickup script

void GenerateBoost()
{   
    int randomNumObject = Random.Range(0,boosts.Length);
    int randomNumRoad = Random.Range(0,roads.Length);
    Vector3 loc = roads[randomNumRoad].transform.position;
    GameObject boost = Instantiate(boosts[randomNumObject], loc, Quaternion.identity) as GameObject;
    boost.GetComponent<Boost>().SetminimapIcon(miniMap.GenerateMiniMapBoost(loc,randomNumObject,boost));

    Destroy(boost,15);
}

Now, when the boost game object gets destroyed it will also destroy its own minimap icon.

1 Like

That works! There’s just one problem however. Even tho the icon is not visible on the map, I can still see it in my heirarchy for some reason. When I click on it, I just see a blank image. Ofc this doesnt affect the gameplay but I was just wondering why it happens

1 Like

Oh, that’s my fault. We only delete the image component. Change the Destroy(minimapImage) to Destroy(minimapImage.gameObject)

1 Like

Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms