How to destroy instantiate gameobject with same position?

Hi.
I have try to switch gameobject with UI button. When press the button on the right side , gameobject with the same look on the button will appear but if i try to change it will stacking instant. i have try to destroy it but i might go down the wrong way because it destroy ref prefab not the clone one.

any suggetion would be nice, thank you.

Hi Puttichawat,

Is there a reason why you destroy the game objects? Perhaps you could enable/disable them with SetActive().

Alternatively, store the reference to the instantiated ball in a variable in a persistent game object. Maybe to presentBall? Then you can destroy the object by passing on the reference to the Destroy method. If you do not do that, you will have to look for the ball object in your scene.

Hi, Nina
I need to toggle between game object for player to choose and it have to show in the same position. At first I thought of using the object that the player chose to shoot it out, but when I got the problem above I changed the method to press to fire the prefab instead. Which it can shoot, but just now there is nothing to show I was thinking of trying to show as a sprite instead. Sorry for the sound


I try to do your suggest by store some sprites in array and toggle it by onclick button but i think i’m not quiet understand how and i know it’s wrong. Can you show me how? Capture4

Don’t you have different types of balls or do you just want to change the sprite? If you just have different sprites, your solution is fine. If you have different types of balls, create an empty Ball game object in your Hierarchy, set the position to (0, 0, 0). Assign your SetBall script to that game object.

Then assign your different balls as child game objects.

Ball (Parent)
> Ball 1
> Ball 2
> Ball 3
> ...

The ball types have everything they need to be a complete, independent ball.

In your SetBall class, you create an array of GameObjects. Assign the child game objects to the SetBall component attached to the parent game object.

For example like this:

[SerializeField] private GameObject[] balls;

private GameObject activeBall;

void Awake()
{
    activeBall = balls[0];
}

public void ChangeBall (int index)
{
    GameObject nextBall = balls[index];

    activeBall.SetActive(false);
    nextBall.transform.position = activeBall.transform.position;
    activeBall = nextBall;
    activeBall.SetActive(true);
}

If you want to instantiate balls, you could assign prefabs to the array. In that case, you’ll need a variable of the instantiated ball, so you can destroy it.

GameObject ball;

void DoSomething()
{
    ball.SetActive(false);
    Destroy(ball);

    GameObject newBall;
    newBall = Instantiate(ballPrefab, ball.transform.position);
    ball = newBall;
}

Hopefully, this helps. Please feel free to ask our helpful community of students for advice in our official Discord chat. :slight_smile:


See also:

I see, I understand now. I use my SetBall script for only toggle sprites as you said and I use another script for instant a ball prefab to shoot only. That’s my solution and it’s work but still can shoot only one type of it. Your solution is very helpful and its already the answer how to toggle prefab. It seem i can use this to shoot more than 1 type. Thank you so much.

I’m glad I was able to help. :slight_smile:

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

Privacy & Terms