- Home /
issue with a random script c# (help)
Good evening everyone,
i need some help with this code. It says "the name instantiate does not exist in the current context" why is this and can you offer a fix for this code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonoBehaviour
{
public GameObject[] objectsToSpawn;
public Transform[] spawnPoints; // An Vector3 array can also be used
public List<GameObject> spawnedObjects; // Containing all spawned Objects; Using List to simply call .Add(GameObject);
public int spawnCount; // How many objects should be spawned
private int objectIndex; // Random objectsToSpawn index
private int spawnIndex; // Random spawnPoints index
private void start ()
{
// Use this for loop to not hardcode the spawn count
for (int i = 0; i < spawnCount; i++)
{
// For each iteration generate a random index; You could make an int array containing if an object already got spawned and change the index.
objectIndex = Random.Range(0, objectsToSpawn.Length);
spawnIndex = Random.Range(0, spawnPoints.Length);
// Instantiate object
GameObject go = Instantiate(objectsToSpawn[objectIndex], spawnPoints[spawnIndex].position, Quaternion.identity);
// Add Object to spawnedObjects List
spawnedObjects.Add(go);
}
}
}
thank you.
Answer by rd_mcn · Mar 17, 2018 at 04:51 AM
First and foremost, you haven't named your class; you have it named after the class you intended to inherit from. So, call it something like:
public class MyClass : Monobehaviour
Although you seemed to have named your file sss.cs, which doesn't follow C# naming practices which is an initial capital letter and camel-case thereafter.
Hope this helps.
wow....it was that simple....i feel pretty stupid. thank you
Please don't... If you want to check out some of my clangers, look up my own questions posted on this form :-)
Your answer

Follow this Question
Related Questions
Can't set the random position for an Instance. 1 Answer
Find a random location, spawn an object there, do it again. 0 Answers
Distribute terrain in zones 3 Answers
How To NOT Instantiate the same Prefab Twice (C#) 1 Answer
why wont the instance of the object move after its been instantiated. 1 Answer