How would I go about spawning a spot light at runtime?

I have a developer console system that I have set up, and I have gotten it to recognise commands properly. My issue now is the code I am using to spawn a “testlight” (spotlight) doesn’t seem to want to behave properly.

I get this error:

Error CS0118: ‘UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)’ is a ‘method’ but is used like a ‘type’

from mono, but this error:

“Object reference not set to an instance of an object”

and this error:

error CS0426: The nested type Instantiate' does not exist in the type UnityEngine.GameObject’

from unity. This is the code for that section below.


public static CharacterController KGCC;
public static void spawnSpot()
{	
	GameObject TestSpot;
	TestSpot = new GameObject.Instantiate(TestSpot, KGCC.transform.position, KGCC.transform.localRotation) as Light; // error on this line
	TestSpot.AddComponent();
	TestSpot.light.color = Color.white;
	TestSpot.light.type = LightType.Spot;
	TestSpot.light.spotAngle = (160);
	TestSpot.transform.position = KGCC.transform.position;
}

can someone provide an example of the proper way to go about this please?

The convention around here is to use lowerCase for variable names and UpperCase for classes.

Anyway, you are trying to Instantiate a TestSpot. There is no TestSpot. Just make a new GameObject, then AddComponent a light and set it to spot and all that. Did you read the manual for AddComponent?

I solved my issue. I had to move my light spawning function into the main console class, instantiate a pre-made prefab and spawn it that way. Works like a charm now.

Thanks for the help anyways!

If anyone wants, I can post the relevant code in case anyone needs it.