Creating an object near player?

Hey it’s David!

I have abit problem coding an attack. What i want is when the player press the “Fire1” button a sword will appear near the player but the player will be moving so it can’t be a specfic spot.
How would i make a sword appear where the player front view at?

This is probably embarrasing to show, but here’s my small attempt.

var weapon : GameObject;

function OnMouseClick ("Fire1"); 
{
    weapon = new GameObject("sword");
    weapon.AddComponents("BoxCollider");
}

First of all, you should use Instantiate instead of new GameObject ()

Second, you can place it relative to the player using:

var weapon : GameObject;

var distance : float=2.0f;

function Update () {
    
    if (Input.GetButtonDown ("Fire1")) {
        var wep : GameObject = Instantiate (weapon, transform.position+transform.forward*distance, transform.rotation);
    }

}

Alright thanks i have that down. Now i forgot to say after a period of time how do i make it delete. I don’t the code for the timer but the delete would be

Destroy(gameObject);

var lifeTime : float=5.0f;

function Whatever () {

    Invoke ("Death", lifeTime);

}

function Death () {

    Destroy (gameObject);

}

Would i put that on the player or on the sword because it’s not working?

I was showing you how to use Invoke.

You could store the sword in a variable and Invoke the Death function directly after Instantiation like so:

var weapon : GameObject;

var distance : float=2.0f;
var lifeTime : float=5.0f;
private var destroyWeapon : GameObject=null;

function Update () {
    
    if (Input.GetButtonDown ("Fire1")) {
        destroyWeapon = Instantiate (weapon, transform.position+transform.forward*distance, transform.rotation);

        Invoke ("Death", lifeTime);
    }

}

function Death () {

    Destroy (destroyWeapon);

}

Or you can put it on the sword using:

var lifeTime : float=5.0f;

function Start () {

    Invoke ("Death", lifeTime);

}

function Death () {

    Destroy (gameObject);

}

I tested it works but once the gameobject weapon is destroy the fire1 doesn’t create anymore sword it gave me an error

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/BaseClass.cs:46)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/BaseClass.cs:57)
UseWeapon.Update () (at Assets/AI/UseWeapon.js:8)

You need to add a check to see if there is a sword.

if (sword != null) {

}

Or, you can rearrange your functions so that it never references the sword unless it needs to (in which case it should be there)

So if the weapon isn’t null it does what?

I’m kinda lost right now where would the null fit?

If it isn’t null, it means that its still there and can be used.

If its null, it means that it has either been destroyed, or was never created.

You are getting the error because you are trying to access the sword when it is null. If you put the code inside the check, you should not get the error because it will only be run when the sword is available.

Would i put the Sword model inside the scene? If i dont it doesn’t destroy after some time because when i use this

But if i do it destroy but after it does i can’t make anymore swords

var weapon : GameObject;

var distance : float=2.0f;
var lifeTime : float=5.0f;
private var destroyWeapon : GameObject=null;

function Update () {
    
    if (Input.GetButtonDown ("Fire1")) {
        destroyWeapon = Instantiate (weapon, transform.position+transform.forward*distance, transform.rotation);

        Invoke ("Death", lifeTime);
    }

    function Death () {

    Destroy (destroyWeapon);

    }

}

i get some errors and the null does work but it still doesn’t delete the sword object after some times

The Sword should be a prefab.

EDIT: I found a mistake in my script. Death should not be inside Update. I fixed it in my above post.

EDIT2: I believe I found your problem. When you create 2 copies of the sword (you click twice), only 1 will get destroyed. Here is the new script:

var weapon : GameObject;

var distance : float=2.0f;
var lifeTime : float=5.0f;
private var destroyWeapon : GameObject=null;

function Update () {
    
    if (Input.GetButtonDown ("Fire1")) {
	
		if (destroyWeapon == null) {
			destroyWeapon = Instantiate (weapon, transform.position+transform.forward*distance, transform.rotation);

			Invoke ("Death", lifeTime);
		}
    }

}

function Death () {

    Destroy (destroyWeapon);

}

Oh man Im so Stupid! It was a model all this time!

Thanks A Bunch Dman, It works now :slight_smile:

You’ve saved a beginner coder a bunch of time, Just can’t thank you enough!!

Anytime :slight_smile:

Check my above post again in case you missed my updated script (edited it in) which doesn’t let you create multiple copies.

Ooh i see, but it still let’s you create mulitple copies if you spam the mouse click