x


How to place a non monobehaviour script into an array in inspector

It may be that I am tired but I cannot seem to figure out how to place a script that doesnt inherit from monobehaviour into an array of that type in the editor/inspector

Example:

[System.Serializable] public class BaseWeapon { public string Name {get; set;} public int Damage {get; set;} public int Ammo {get; set;}

  // Constructor
  public BaseWeapon BaseWeapon(string name, int dmg, int ammo)
  {
     Name = name;
     Damage = dmg;
     Ammo = ammo;
  }

}

// DIFFERENT SCRIPT public class WeaponPickup : MonoBehaviour { public BaseWeapon[] weapons;

private BaseWeapon weaponToDrop;

void Awake() { if(weapons.Length > 1) weaponToDrop = GetRandomWeapon(); else weaponToDrop = weapons[0]; }

void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "Player") { other.sendMessage("GetWeapon", weaponToDrop); } }

}

// DIFFERENT SCRIPT public class Handgun : BaseWeapon { public BaseWeapon weapon = new Baseweapon("Handgun", 5, 30); }

// DIFFERENT SCRIPT public class PlayerScript : MonoBehaviour { public Projectile projectile;

private BaseWeapon equippedWeapon; private BaseWeapon[] weapInventory;

void GetWeapon(BaseWeapon weap) { int i = weapInventory.Length; weapInventory[i] = weap; equippedWeapon = weapInventory[i]; }

void Update() { if(Input.GetAxis("Fire1") { FireWeapon(); } }

void FireWeapon() { if(equippedWeapon.Ammo > 0) { GameObject tempProj = (GameObject)Instantiate(projectile, transform.position,
transform.rotation); Projectile proj = tempProj.GetComponent("Projectile"); tempProj.damage = equippedWeapon.Damage; equippedWeapon.Ammo--; } } }

Now in the above code I have a MonoBehaviour script attached to a GameObject and it displays the weapons array but when I add more elements to it I cant just drag the BaseWeapon script to the element.

Im wanting to do this so the level designers can easily drag the weapon that they want the pickup to drop into the array, theres also code in place to select one at random if more than one weapon is in the array.

EDIT: Added additional code, Projectile is just a monobehaviour which has a public var damage and simply moves the go forward and sends the damage variable to whatever it hits. This code is not my original as I am not at home atm so i just hand typed this myself and it is not complete such as there is also a function to allow the player to cycle thru their weapons but i left it out for this... If any additional info is needed let me kno

Thanks in advanced

more ▼

asked Feb 28, 2011 at 03:53 AM

unitydev0008 gravatar image

unitydev0008
162 39 30 39

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You seem to have a misunderstanding of what

public BaseWeapon[] weapons;

is. It is an array of references to instantiated objects, not an array of classes. If you have a prefab with a BaseWeapon object attached to it, then you can drag that prefab onto a slot in the weapons array. If that's not what you want to do, my next guess would be that you want an array of enums. If this isn't enough to get you going, further description and code for what you want to do will be necessary to help you.

more ▼

answered Feb 28, 2011 at 03:29 PM

Jessy gravatar image

Jessy
18.4k 208 178 293

Hi Jessy,

What I am trying to do is develop a Top down shooter game, the baseWeapon class is the default class that I derive from when I make a new weapon script, this class holds all the base stats of weapons, such as the name, rate of fire, accuracy, etc... The array I am making in weapon pickup is of type base weapon so that I can drag the weapons created that inherit from baseweapon into the array so the weaponPickup can "Drop" that weapon. This array is used to send the weaponscript to the player and add that weapon to their inventory so it can be used by the player...Thanks again

Feb 28, 2011 at 07:17 PM unitydev0008

Again, code will help us help you. "weapons created", and "send the weaponscript to the player" are not clear phrases to me.

Feb 28, 2011 at 08:35 PM Jessy

Added additional code which was hand typed and not in VS so it may not be correct but should help in understanding what im trying to do...thanks again for helping Jessy

Mar 01, 2011 at 12:03 AM unitydev0008

Hey man thanks for the help, however I have decided to go about it a different way by just having a monobehaviour class create an instance of my base weapon, seems to be working just fine...thanks again man and i accepted ur answer and +1 to show my gratitude.

Mar 02, 2011 at 03:00 AM unitydev0008

I'm sorry I didn't help you more directly; I got REALLY busy. Glad to hear you have a good solution now.

Mar 02, 2011 at 06:33 AM Jessy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2247
x818
x242

asked: Feb 28, 2011 at 03:53 AM

Seen: 1788 times

Last Updated: Mar 01, 2011 at 12:02 AM