B
B
BadCats2017-08-11 23:49:33
C++ / C#
BadCats, 2017-08-11 23:49:33

Script interaction with other objects in 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))‌​;

do not tell me why I have a NullReferenceException here?
This is despite the fact that in
GameObject respawn = GameObject.FindGameObjectWithTag("Respawn");

- the tag on the object is assigned to me;
well, more precisely, it swears at ps.transform.position = Input.mouseScrollDelta;
but gives exactly NullReferenceException - therefore it swears at the line
ParticleSystem ps = (ParticleSystem)respawn.GetComponent(typeof(ParticleSystem))‌​;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheTalion, 2017-08-12
@BadCats

So there is no ParticleSystem component on the respawn object. Use a debugger to find exactly what is missing. When you understand that when the function is called null, then the further algorithm for solving the problem will be clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question