Why game object?

Why did we made it a game object in "gameobject laser = ". Why not let it be a prefab?

 private void Fire()
    {
        if (Input.GetButtonDown("Fire1"))
        {
           GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
            laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
        }
    }

Hi Prajaney,

The Instantiate method doesn’t create a prefab but a game object in the scene. We need the laser variable to be able to access the instantiated object.

Did this clear it up for you?

yes, thank you very much :slightly_smiling_face: :slightly_smiling_face:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.