Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jsvalentin · Feb 22, 2019 at 12:46 PM · programmingobjectsenumstructure

What structure code is this and how can i use it with enum?

I have an issue I'm trying to figure out what structure code is this found it in javascript and trying to convert it to C# I made it work by replacing {} to () and made it work as an object See second example.

The main issue is how can I use it in such way that will take enum to get correct value

Example: public enum Type { None, Normal, Fighting, Fire, Water, Grass, }

player.type1 is a value from enum "Fire". rival.type1 is a value from enum "Grass".

I would like to use it as Debug.Log("The value is:" Effectiveness + player.Type + rival.Type) In result i would like to get value 2.

Javascript Objective-oriented Programming with out class example below var Effectiveness= { Fire: { Normal: 1, Fire: 0.5, Water: 0.5, Grass: 2 }, Water: { Normal: 1, Fire: 2, Water: 0.5, Grass: 0.5 } };

Unity C# work around it it work fine but first time using it see example below

var Effectiveness= ( Fire: ( Normal: 1, Fire: 0.5, Water: 0.5, Grass: 2 ), Water: ( Normal: 1, Fire: 2, Water: 0.5, Grass: 0.5 ) ); Debug.Log(Effectiveness.Fire.Grass); // Will spit out result as 2

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
0
Best Answer

Answer by Hellium · Feb 22, 2019 at 01:08 PM

You could use a simple 2D array. It can be a little bit tedious to fill, but you could use a file you will be able to load the data from (formatted in CSV for instance). You could also use a ScriptableObject with a custom editor to format it in the inspector.


Following code not tested:

 using UnityEngine;
 using System;

 public enum Type
 {
      Normal,
      Fire,
      Water
 }
 public MyMonoBehaviour : MonoBehaviour
 {

      private int[,] effectiveness ;
      private void Start()
      {
         int enumLength = Enum.GetNames(typeof(Type)).Length;
         effectiveness = new int[enumLength, enumLength];
         SetEffectiveness( Type.Normal, Type.Fire, 1 );
         // ....
         Degug.Log( GetEffectiveness( Type.Normal, Type.Fire ) );
      }
     
     public int GetEffectiveness( Type type1, Type type2 )
     {
          return effectiveness [(int) type1,(int) type2];
     }
     public void SetEffectiveness( Type type1, Type type2, int value )
     {
         effectiveness [(int) type1,(int) type2] = value;
     }
 }
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

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Listing multiple objects 2 Answers

Creating different damage types with enums switches 0 Answers

Problem with getting a value from a enum 2 Answers

I'm already an experienced programmer, is there anywhere great to go to get familiar with the libraries available in unity? 2 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