BadCats
@BadCats

Взаимодействие скрипта с другими объектами в unity?

GameObject respawn;
    ParticleSystem ps;
 void Start() {
        gb = GetComponent<Transform>();
        camera1 = Camera.main;
        camera2 = GetComponent<Camera>();
        camera2.enabled = false;
        camera1.enabled = true;
        respawn = GameObject.FindGameObjectWithTag("Respawn");
        ParticleSystem ps = (ParticleSystem)respawn.GetComponent(typeof(ParticleSystem));
    }
 void Update() {
if (Input.GetMouseButton(0) && gb.transform.rotation.x != 180 && gb.transform.rotation.y != 90)
        {
           
        
            gb.LookAt(Input.mousePosition);
            gb.Rotate(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f),0f,Space.World);
            ps.transform.position = Input.mouseScrollDelta;
            ps.Play();

        }


ParticleSystem ps = (ParticleSystem)respawn.GetComponent(typeof(ParticleSystem))‌​;

не подскажите, а почему у меня тут возникает NullReferenceException?
Это при том что в
GameObject respawn = GameObject.FindGameObjectWithTag("Respawn");

- тэг на объекте у меня присвоен ;
ну точнее ругается на ps.transform.position = Input.mouseScrollDelta;
но выдает именно NullReferenceException - следовательно ругается на строку
ParticleSystem ps = (ParticleSystem)respawn.GetComponent(typeof(ParticleSystem))‌​;
  • Вопрос задан
  • 214 просмотров
Решения вопроса 2
TheTalion
@TheTalion
Значит нет компонента ParticleSystem на объекте respawn. Пользуйтесь отладчиком, чтобы найти точно чего нет. Когда поймете что при вызове функции null, тогда дальше алгоритм решения задачи будет понятен.
Ответ написан
Griboks
@Griboks Куратор тега C#
Эмм, а почему везде GetComponent<T>(), а у ps GetComponent(T)

ParticleSystem ps = (ParticleSystem)respawn.GetComponent<ParticleSystem>();
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы