7

How to change gameobjects position if it's already used by a different gameobjec...

 2 years ago
source link: https://www.codeproject.com/Questions/5320358/How-to-change-gameobjects-position-if-its-already
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Gameobjects are getting randomly spawned on some platform objects. I want to avoid these two different gameobject to be spawned at the same exact location (money2 should change it's position).

Here is the code:

Copy Code
void Start()
{
    int randMoney = Random.Range(0, 8);
    Vector3 moneyPos = transform.position;
    moneyPos.y += 0.5f;
    if (randMoney < 1)
    {
        GameObject moneyInstance = Instantiate(money, moneyPos, money.transform.rotation);
        moneyInstance.transform.SetParent(gameObject.transform);
    }

    int randMoney2 = Random.Range(0, 8);
    Vector3 money2Pos = transform.position;
    money2Pos.y += 0.5f;
    if (randMoney2 < 1)
    {
        GameObject money2Instance = Instantiate(money2, money2Pos, money2.transform.rotation);
        money2Instance.transform.SetParent(gameObject.transform);
    }

    if (money2Pos == moneyPos)
    {
        //where gameobject2s position should change
    }
}
Thank you for taking your time!

What I have tried:
Copy Code
if (money2Pos == moneyPos)
        {
            //where gameobject2s position should change
        }

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK