- Home /
How to substitute an array for a public Gameobject?
Hello, I'm trying to get a weapon array to work so I can have my player shoot different prefabs. If I keep the public field as one prefab it works fine, I tried to add the array and it's not working. Can someone point me in the right direction? Thanks (and by the way I realize there is currently nothing to change the currentWeapon int from 0 to 1, but I've been doing it manually to test it)!
// Launch projectile
public class PlayerWeaponScript : MonoBehaviour
{
public Transform [] shotPrefab;// Projectile prefab for shooting
public float shootingRate = 0.25f;// Cooldown in seconds between two shots
private float shootCooldown;
private int currentWeapon = 0;
void Start()
{
shootCooldown = 0f;
}
void Update()
{
if (shootCooldown > 0)
{
shootCooldown -= Time.deltaTime;
}
}
//Shooting from another script
// Create a new projectile if possible
public void Attack(bool isEnemy) {
if (CanAttack) {
if (currentWeapon == 0) {
int shotIndex = 0;
} else if (currentWeapon == 1) {
int shotIndex = 1;
shootCooldown = shootingRate;
var shotTransform = Instantiate (shotPrefab [shotIndex]) as Transform;// Create a new shot
shotTransform.position = transform.position;// Assign position
ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript> ();
if (shot != null) {
shot.isEnemyShot = isEnemy;
gameObject.tag = "Enemy";
}
// Make the weapon shot always toward
EnemyMoveScript move = shotTransform.gameObject.GetComponent<EnemyMoveScript> ();
if (move != null) {
move.direction = this.transform.right; // towards in 2D space is the right of the sprite
}
}
}
}
public bool CanAttack// Is the weapon ready to create a new projectile?
{
get
{
return shootCooldown <= 0f;
}
}
}
@Danao diid you get any error? Or just generate same prefab every time?
Thanks for the reply yashpal. It's not generating any prefab when I use the array (though I can use a single public gameObject and it works fine for one weapon).
$$anonymous$$essed with your formatting a bit. Need to be able to read it to help. :)
Answer by Danao · Nov 03, 2014 at 06:22 AM
Here is the code that finally worked for me (still changing the currentWeapon int manually), based on major help from BoredMormon and Bunny83:
using UnityEngine;
// Launch projectile
public class PlayerWeaponScript : MonoBehaviour
{
public Transform [] shotPrefab;// Projectile prefab for shooting
public float shootingRate = 0.25f;// Cooldown in seconds between two shots
private float shootCooldown;
private int currentWeapon = 0;
void Start()
{
shootCooldown = 0f;
}
void Update()
{
if (shootCooldown > 0)
{
shootCooldown -= Time.deltaTime;
}
}
//Shooting from another script
// Create a new projectile if possible
public void Attack(bool isEnemy) {
if (CanAttack){
if (currentWeapon == 0) {
} else if (currentWeapon == 1) {
}
shootCooldown = shootingRate;
var shotTransform = Instantiate (shotPrefab [currentWeapon]) as Transform;// Create a new shot
shotTransform.position = transform.position;// Assign position
ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript> ();
if (shot != null) {
shot.isEnemyShot = isEnemy;
gameObject.tag = "Enemy";
}
// Make the weapon shot always toward
EnemyMoveScript move = shotTransform.gameObject.GetComponent<EnemyMoveScript> ();
if (move != null) {
move.direction = this.transform.right; // towards in 2D space is the right of the sprite
}
}
}
public bool CanAttack// Is the weapon ready to create a new projectile?
{
get
{
return shootCooldown <= 0f;
}
}
}
Answer by Kiwasi · Nov 03, 2014 at 03:18 AM
Looks like you need another } to close off the else if on line 29. Currently you are only firing if the second weapon is selected. A nested if might be better. Pseudo code
public void Attack(bool isEnemy) {
if (CanAttack){
int shotIndex;
if (currentWeapon == 0) {
shotIndex = 0;
} else if (currentWeapon == 1) {
shotIndex = 1;
}
// Do attack as written
}
}
Note: If currentWeapon is always going to map directly onto shotIndex then there is no need to use shotIndex.
Edit: Moved scope of shotIndex up a level. See comments.
Thanks for the reply Bored$$anonymous$$ormon. I tried the pseudo code you provided and updated my original code posting accordingly, but it still seems I can only fire the second weapon. If shotIndex = 0, it should pull the prefab from element 0 in the array, correct?
Your updated code still has the shooting inside the else if clause
Yes, looks like that is the problem. I tried to put the shooting in both the if and else if, but that messes with my definitions. I'm going to need to try something else. I'll see what I can come up with and come back to post with some (hopefully) working code.
It should be in neither. Pay careful attention to the bracket position in my pseudo code.
Your answer
Follow this Question
Related Questions
Weapon Prefab 0 Answers
How to create Random Stats matching the weapons? 0 Answers
Trying to instantiate random enemy prefab from array 3 Answers
Resizeable gameObject array. 1 Answer
tracking ownership of shots 1 Answer