Home > Enterprise >  How do I add the GameObject to my script?
How do I add the GameObject to my script?

Time:07-15

So I'm writing a script for respawning objects for a 2d game on unity, however whenever i try to call the object that is to respawn, it says that the object is either a method or statement. I feel like I've made a mistake somewhere. Please help correct me where im wrong. This is the code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    public GameObject spike;
    public class Spikegenerator: MonoBehaviour
    {
    void Start()
    {
    for (int i = 0; i < 10; i  )
    {
        float spawnY = Random.Range
            (Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y, 
    Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
        float spawnX = Random.Range
            (Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x, 
    Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x);

        Vector2 spawnPosition = new Vector2(spawnX, spawnY);
        Instantiate(spike, spawnPosition, Quaternion.identity);
    // the gameobject is 'spike'
    }
    }

    }'''

CodePudding user response:

The spike variable u are providing doesn't exist yet in the code.

Make sure you add public GameObject spike Inside your class and insert the prefab spike via the editor.

CodePudding user response:

Make sure you added spike on the inspector of your GameObject.

  • Related