Quick question about IFs

Hello, new Unity user here. New to javascript as well. I am pretty good with Game Maker, but decided to upgrade my video game creating experience. I am very impressed with Unity. Anyways, on to the question. I was following the Lorax’s old tutorial about concept to video game video tutorials, where he makes a simple cube and 2 spheres and makes a top down scrolling shooting game. Well, I decided to try to expand upon it, and make it so that if the user pushes “1” on the keyboard, the “var playerWeapon” = 1, and “2” makes “playerWeapon = 2” … I did a “print” test, and the code works fine for those. The question is, when playerWeapon = 2, I want to create 2 bullet instances, but I keep getting errors. What am I doing wrong? Here is my code.

// fire weapon
if( Input.GetKeyDown(“space”))
{
var tempBullet: Rigidbody;

if (playerWeapon = 1) // if playerWeapon = 1, create 1 bullet in center of player
{
tempBullet = Instantiate(bullet, transform.position, transform.rotation)
}

if (playerWeapon = 2) // if playerWeapon = 2, create 2 bullets on side of player
{
tempBullet = Instantiate(bullet, transform.position.x - .05, transform.rotation)
tempBullet = Instantiate(bullet, transform.position.x + .05, transform.rotation)
}
}

I should probably note, I’m getting 3 errors, lines 51,26 51,28 and 53,28 line 51 is the line

if (playerWeapon = 1) // if playerWeapon = 1, create 1 bullet in center of player

and line 53 is

tempBullet = Instantiate(bullet, transform.position, transform.rotation)

You’re asigning a value with a single equal instead of comparing values. To compare a value you should use IF with double equal:

if (playerWeapon == 1) // if playerWeapon = 1, create 1 bullet in center of player
{
tempBullet = Instantiate(bullet, transform.position, transform.rotation);
}

if (playerWeapon == 2) // if playerWeapon = 2, create 2 bullets on side of player
{
tempBullet = Instantiate(bullet, transform.position.x - .05, transform.rotation);
tempBullet = Instantiate(bullet, transform.position.x + .05, transform.rotation);
}

You missed semicolons too.

Ooooh yea! I remember now him saying if you put = that MAKES the variable into that amount. Thanks for that! (and the semi colons) but now I get this error.

Assets/Scripts/playerScript.js(58,41): BCE0023: No appropriate version of ‘UnityEngine.Object.Instantiate’ for the argument list ‘(UnityEngine.Rigidbody, float, UnityEngine.Quaternion)’ was found.

EDIT: it seems that it only does that on the code for making 2 bullets. Does that mean I have to create 2 temp variables inside the if playerweapon == 2 instead of just the 1 temp variable inside the if keydownpress space code? I’m going to test it and see, but, if you could elaborate on this more, Id appreciate it (just wonder if I am ending up making more work than is needed)

Take a look at Instantiate() function: http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

In your case, you’re using:

tempBullet = Instantiate(bullet, transform.position.x - .05, transform.rotation);
tempBullet = Instantiate(bullet, transform.position.x + .05, transform.rotation);

You need to use a Vector3 instead of float.

transform.position.x + .05 → float
transform.position → Vector3

You have a lot of differents solutions, in your case you can do something like this:

var newPos1 : Vector3 = Vector3(transform.position.x - 0.05, transform.position.y, transform.position.z);
tempBullet = Instantiate(bullet,newPos1, transform.rotation);

var newPos2 : Vector3 = Vector3(transform.position.x + 0.05, transform.position.y, transform.position.z);
tempBullet2 = Instantiate(bullet,newPos2, transform.rotation);

You will need a new variable for the second bullet if you don’t want to lose their reference.

Anyway, I’m using C# instead of Javascript, so I can’t confirm syntax issues and similar errors on this snippet.

Ok I think I understand now. Vector3 is used when changing positions for a particular object in 3d space. I’ll definitely read up more on the Instantiate() section in the manual! Thank you for all your help!

EDIT: Works beautifully! Once again, thank you for your help! :slight_smile: