Instantiate JavaScript

Hello,

Im trying to make a little script that whenever i press K, an object spawns infront of my character, this is what i have at the moment;

#pragma strict

public var Cube;

function Start () {

}

function Update ()
{
    if(Input.GetKeyDown(KeyCode.K))
    {
        print("K is pressed");
        for (var i = 0; i < 10; i++) {
            Instantiate(Cube, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
            i++;
        }
    }
}

function OnTriggerEnter(col : Collider)
{
    if(col.gameObject.name == "Cube")
    {
        print("Cube");
        Destroy(col.gameObject);
    }
}

But the problem is, whenever i press K, it says

There is an prefab named; Cube in the hierarchy.

I have no clue what the problem is,

Can somebody help me?
Thankyou!!

-Luc

have a look at the object this script is attached to, there will be a slot called “Cube” which you should drag the prefab from you project view into.

public vars at the top make slots like that you have to fill. Having an instance of the prefab in the hierarchy doesn’t do anything in this regard.

this is the only thing i see

did you save the changes to the script? are there any errors in the console which are stopping the script from saving?

Yes i saved it, and no there are no errors occuring

ah,

public var Cube : Transform;

needs to know the type to make the slot in the inspector…

sorry, c# doesn’t let you do that, my jsfoo is weak these days lol :frowning:

Get no errors, but on pressing K i get:

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)

And yes, i added the Cube :smile:

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” :slight_smile:


Thanks for the tip :smile:, This is what i have compared to the error message

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…

only way i can generate the error is by setting the slot to none. :eyes:

save, close, reopen and try again, maybe something is squiffy.

really ._.

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.

When i press K;


He makes 4 cubes at a time, on the edge of the terrain…
It has to be one cube, on the same position as the player :open_mouth:

EDIT; fixed the duplicating, only spawns 1 in now, but how to make it spawns on my player coordinates?

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);
}