Добавить скрипт к новому игровому объекту во время выполнения - PullRequest
0 голосов
/ 12 марта 2020

Я делаю игру в единстве ... Мой сценарий разрезает тело меня sh на 2 других тела меня sh. Как я могу добавить скрипт в новое тело во время выполнения?

В каждый блок необходимо добавить скрипт с именем XRGrabInteractable.

foreach(GameObject chunk in pieces){
                if(chunk.GetComponent<BoxCollider>()){//change to collider type of first object
                    Destroy(chunk.GetComponent<BoxCollider>());
                }
                //Add rigid body if not alread
                if(!chunk.GetComponent<Rigidbody>()){//Add Rigid body if not true
                    chunk.AddComponent<Rigidbody>();

                }

                //Add Mesh Colider(might interere with xr script that requires collider)
                if(!chunk.GetComponent<MeshCollider>()){//If no mesh colider mesh colider for new chunk
                    chunk.AddComponent<MeshCollider>();//Add Mesh colider if it doesn't exist
                    chunk.GetComponent<MeshCollider>().convex = true;//Change To Convex Mesh so that it doesn't fall through floor
                }
}

Ответы [ 2 ]

2 голосов
/ 12 марта 2020

Использование GameObject.AddComponent<ScriptName>():

foreach(GameObject chunk in pieces){
    chunk.AddComponent<XRGrabInteractable>();

    if(chunk.GetComponent<BoxCollider>()){//change to collider type of first object
        Destroy(chunk.GetComponent<BoxCollider>());
    }

    //Add rigid body if not alread
    if(!chunk.GetComponent<Rigidbody>()){//Add Rigid body if not true
        chunk.AddComponent<Rigidbody>();
    }

    //Add Mesh Colider(might interere with xr script that requires collider)
    if(!chunk.GetComponent<MeshCollider>()){//If no mesh colider mesh colider for new chunk
        chunk.AddComponent<MeshCollider>();//Add Mesh colider if it doesn't exist
        chunk.GetComponent<MeshCollider>().convex = true;//Change To Convex Mesh so that it doesn't fall through floor
    }
}
0 голосов
/ 12 марта 2020

Оказывается, мне просто нужно было использовать ...

using UnityEngine.XR.Interaction.Toolkit;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...