Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unitydev0008 · Feb 28, 2011 at 03:53 AM · arrayinspectormonobehaviour

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jessy · Feb 28, 2011 at 03:29 PM

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unitydev0008 · Feb 28, 2011 at 07:17 PM 0
Share

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

avatar image Jessy · Feb 28, 2011 at 08:35 PM 0
Share

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

avatar image unitydev0008 · Mar 01, 2011 at 12:03 AM 0
Share

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

avatar image unitydev0008 · Mar 02, 2011 at 03:00 AM 0
Share

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.

avatar image Jessy · Mar 02, 2011 at 06:33 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Display Non Monobehaviour array in inspector C# 1 Answer

Arrays and the inspector 1 Answer

Array not updating in inspector 0 Answers

How to make a custom type's contents modifiable in an array by inspector? 1 Answer

have i derive from MonoBehaviour to expose fields in inspector? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges