UnassignedReferenceException: The variable Cube of onpickup has not been assigned.
You probably need to assign the Cube variable of the onpickup script in the inspector.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:74)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:84)
onpickup.Update () (at Assets/Scripts/onpickup.js:15)
complete side note, variables are generally lower case first letters. Classes and functions are usually upper case first letter. So really it should be “OnPickup” and “public var cube : Transform”
odd, I’ve made a test project with that script with the slight tweak. Made a cube, dragged it from the hierarchy into the project to create a prefab. Created an empty and put that script on it, dragged the prefab from the project view into the slot and run. hit k and lots of cube appear…
Maybe, he spawns them in the first 2 times?? and then gets errors after repeating pressing K, (Dont see any objects in the game tbh) But my landscape is also like with hills idk if thats a problem too?
shouldn’t happen if its using a prefab from the project view… did you assign a cube from the hierarchy, and does that cube get destroyed at some point?
There is one “Cube” in the game, that gets destroyed when i touch it ( that was a side code haha )
There is a “Cube” in the Project folders that was assigned from project folder too the script Transform.
Got it working, it spawns now on my character, but i would like to have it spawned on the place where my character is looking, is that possible? [Script now]
#pragma strict
public var cube : Transform;
private var playerPos:Vector3;
private var spawnPos:Vector3;
public var player: GameObject;
function Start () {
}
function Update ()
{
if(Input.GetKeyDown(KeyCode.K))
{
print("K is pressed");
DoInstantiate();
}
}
function OnTriggerEnter(col : Collider)
{
if(col.gameObject.name == "Cube")
{
print("Cube");
Destroy(col.gameObject);
}
}
function DoInstantiate(){
playerPos = player.transform.position;
spawnPos = Vector3(playerPos.x, playerPos.y-0.5, playerPos.z); ///change as needed
Instantiate (cube, spawnPos, Quaternion.identity);
}