Nobert
Nobert

Reputation: 163

Unity: the name 'Instance' does not exist in the current context

I was following a tutorial on how to create a 2D enemy spawner in unity the problem is that it gives me the error

the name 'Instance' does not exist in the current context

when i run this code

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

public class SpawnEnemys : MonoBehaviour
{
    public GameObject enemy;
    float randX;
    float randY;
    Vector2 whereToSpawn;
    public float spawnRate = 2f;
    float nextSpawn = 0.0f; 


    void Update()
    {
        if (Time.time > nextSpawn)
        {
            nextSpawn = Time.time + spawnRate;
            randX = Random.Range(-6.36f, 6.36f);
            randY = Random.Range(-4.99f, 4.99f);
            whereToSpawn = new Vector2(randX, randY);
            Instance (enemy, whereToSpawn, Quaternion.identity);
        }
    }
}

I Googled it and read some documents but they didn't help. Why do i get the error and how do i stop it?

Upvotes: 0

Views: 1471

Answers (2)

Nobert
Nobert

Reputation: 163

You have a typo. Method you should call is Instantiate, not Instance
@Iliar Turdushev

Upvotes: 0

Frenchy
Frenchy

Reputation: 17007

the error means you want to call a method, but it doesnt exists, so you have to create the method Instance like this:

public class SpawnEnemys : MonoBehaviour
{
    :
    : 
   public void Instance(GameObject enemy, Vector2 wheretoSpawn, Quaternion rotation)
   {
       //Instantiate your Gameobject
       Instantiate(enemy, wheretoSpawn, rotation);
   }
    :
    :
   void Update()
   {
       if (Time.time > nextSpawn)
       {
          :
           Instance (enemy, whereToSpawn, Quaternion.identity);
       }
   }


}

Upvotes: 1

Related Questions