简体   繁体   中英

unity How to spawn different objects in the same location?

Lets say I have six basketballs of different colors in this game im trying to make.

and I shoot these basketballs one at a time. This game will only display the rim and the ball. and if I miss or make the shot,I want the ball to be destroyed and I want another color ball or same color ball to spawn in the same place as the first one and if I miss or make the shot then I want the next ball to spawn in the same location and so on... until the game is done.

You're description gives me a lot that I don't need to know, and doesn't give me a lot of what I need to know.

It's going to look something like this:

using System;
using Unity.Engine;

public Vector2 SpawnLocation;
public GameObject BasketBall;

public class BasketBallSpawner : MonoBehavior
{
    public void Update()
    {
        if (needToSpawnBall) spawnBall();
    }

    public void spawnBall()
    {
        GameObject basketBall = Instantiate(BasketBall, SpawnLocation, 0);
    }
}

Note that I this is pseudo code, and it does not include the part of getting one of the six colors randomly. Do that, you might include an array of six basketballs in your public fields, or change the color programactically, that is up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM