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
1
Question by D3Duck · Apr 08, 2015 at 09:58 AM · childclassesinstances

How to show instance variables in the inspector

I am trying to use a system of base and derived classes in Unity for the first time.

What I can't figure out is how I can show instance variables in the inspector, if that is possible at all. Possibly I'm doing this completely wrong from the start. My intention was to set up a system where I can have spells derive from the base class and build on them, and then use a script on the player to instantiate these attacks. So I don't need an entire, almost duplicate, script for each attack.

I need them in the inspector so that I can assign prefabs for, for example, particle systems that are specific to each attack.

This is the beginning that I've made:

 using UnityEngine;
 using System.Collections;
 
 
 public class AttacksHandler : MonoBehaviour
 {
     GameObject playerGO;
 
     public class Spell
     {
         public bool needsTarget;
         public float movementSpeed;
         public int damage;
         public GameObject attackVisual; //HOW TO SHOW THIS THING IN INSPECTOR FOR EACH INSTANCE?
 
         public Spell(bool _needsTarget, float _movementSpeed, int _damage)
         {
             needsTarget = _needsTarget;
             movementSpeed = _movementSpeed;
             damage = _damage;
         }
     }
 
     public class Fireball : Spell
     {
         public ParticleSystem explosionEffect; //HOW TO SHOW THIS THING IN INSPECTOR FOR EACH INSTANCE?
         GameObject target;
 
         public Fireball(bool _needsTarget, float _movementSpeed, int _damage) :
             base(_needsTarget, _movementSpeed, _damage)
         {
         }
     }
 
     public Fireball redFireball = new Fireball(true, 50f, 25);
     public Fireball greenFireball = new Fireball(false, 20f, 200);
 
     void Start()
     {
         playerGO = GameObject.FindGameObjectWithTag("Player");
     }
 }

Again, I may be doing this completely wrong alltogether but this was my idea to use OOP.

Comment
Add comment · Show 1
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 Nerevar · Apr 08, 2015 at 10:30 AM 0
Share

Hi,

I don't know if it will work with the way you wrote this script but if you want to make a custom inspector for you class you should check this. It might give you an idea of how you should organise your class and variables.

Edit : I don't see the point for being able to assign anything via the inspector at runtime (i.e for instanciated classes) because everything will be lost when you stop your scene anyway...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GibTreaty · Apr 08, 2015 at 11:57 AM

Firstly you would need to put [System.Serializable] above each class that doesn't inherit from MonoBehaviour (or classes that do not have Object as their base class). Secondly, instances of your classes will only show up as their base class, Spell, in the inspector.

What you'll want to do is look into ScriptableObjects. You can inherit your spell class from it and still inherit other spells from that spell class.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Creating class instances 1 Answer

Make a simple tree 1 Answer

Name instance String variable after instance? 0 Answers

Regarding selecting objects for Occlusion Culling 0 Answers

Problem accessing public instances 1 Answer


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