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 Nickshepp · Mar 10, 2021 at 03:11 PM · arrayslistsnoob

How can I store different number of values using lists and arrays?

Hi, sorry for the badly worded question, wasn't too sure how to phrase it. (noob alert)
So this is just a learning example. I have four monsters in this example, each with an image, a name and an attack value and this works using arrayindex number (initialized at 0) to cycle through them:

private string[] enemynameslist = { "Alpha", "Beta", "Charlie", "Delta"};
private int[] enemyattack = { 2, 4, 6, 8 };
private int arrayindexnumber;


What I'm struggling with now is how I could do things like the following:

  1. Make it so the Alpha monster attacks for a random range value (I've declared the array as interger of course) - I did consider making it a string array and then converting later.

  2. Make it so the Alpha monster has two attack values, maybe 1 then 2, but really would like to be able to support "as many variations" as I like - I did consider having multiple arrays (attack1, attack2, attack3) etc, and I could probably cobble something together that works but it seems inelegant. Also what if Alpha has two attacks, but Beta has four, or Charlie has 100.


Any advice or reading would be welcome. I'm still in the early stages and sometimes finding the right information is difficult when I don't really know what to look for.

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 Nickshepp · Mar 10, 2021 at 05:55 PM 0
Share

(Scales fall from eyes) Ah I see, sorry I was thrown by the vector 3 related stuff and couldn't see how that applied. Went code blind I think. Thats all really helpful. I'll admit that the whole classes thing is something I definately need to find out more on, but I get the core idea now I've read your code example properly and your last comment was incredibly helpful. Thank you very much, I know what I need to read up on (I've been learning Unity fairly randomly which has taken me so far, but I think its time to go a bit more back to "basics").

for (var i = 0; i < 3; i++)

{

Debug.Log("Thank you highpockets")

}

1 Reply

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

Answer by highpockets · Mar 10, 2021 at 04:01 PM

I don’t really understand exactly what your question is, but I’ll give it a shot. I’m assuming you want to give your enemies the ability to have different attack variations while being randomized.


So in your attack functionality just pick a random attack value where the returned int will be one of the enemyAttacks in the enemyAttacks array which I’m assuming the values in the enemyAttacks array are referring to damage to be served:

 int[] enemyAttacks = { 2, 4, 6, 8, 10 };
 
 void Attack()
 {
     //get a random attack value from enemy attack array
     int damage = enemyAttacks[Random.Range(0,enemyAttacks.Length)];
 
    //deal damage if player is hit.. you’ll have to figure this out
 }








Comment
Add comment · Show 8 · 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 Nickshepp · Mar 10, 2021 at 04:20 PM 0
Share

Hi thanks for the reply, I guess I didn't word it very well. In my example Alpha deals 2 damage (arrayindex 0 applied to both), Beta deals 4 damage (array index 1 applied to both). So the values for each monster is not random but deter$$anonymous$$ed by the matching index is the enemydamage variables. (in this example the four monsters are progressively stronger, dealing 2,4,6 and 8 damage respectively.

But what if I want Alpha to deal 2 damage the first time, then 3 the second and still store this information in an easy adjustable way. I appreciate the method I've done above won't work, looking for suggestions on what to try. Have had a look at lists, arrays, hashtables and none of them seem appropriate.
Perhaps something like an array of lists might do it (if thats possible) so its something like:
private string[] enemynameslist = { "Alpha", "Beta", "Charlie", "Delta"}; "monsterattackarray"={List1, List2, List3, List4}

"List1"=2,3 (alpha's attack list)

"List2"=4 (betas attack list)

"List3"=6,0,6,0 (charlies attack list)

"List4"= 8 (deltas attack list)


But I'm not entirely sure if this is possible, advised, or if theres a better way of doing it, plus how the code would work. Hope that explains it a bit better and seriously thanks for replying, I'm still pretty new and still at the stage where I like simple examples that I can adapt and then expand upon.

avatar image highpockets Nickshepp · Mar 10, 2021 at 05:13 PM 1
Share

If I was to do this, I would define my Monsters in a Monster class, put that class on each monster object, define the values in the editor and then have a monsters array in the monster controller class. You don't have a very complicated setup. (scriptable object for each monster would be good to, but lets keep things simple):


 //Add this to each monster in the editor and assign values accordingly
 public class Monster : MonoBehaviour
 {
     public string name;
     public DamageAmounts[] damageAmounts;

     public void Attack()
     {
         int damage = damageAmounts[Random.Range(0,damageAmounts.Length)];

         //deal damage, animate, etc..
     }
 }



 public class MonstersController : MonoBehaviour
 {
     //Put each monster in the array in the editor
     public Mosters[] monsters;
 
     //Attach player transform to this, just an example
     public Transform player;
 
     private float strikingDist;
 
     void Update()
     {
         for(int i = 0; i < monsters.Length; i++)
         {
             if(Vector3.Distance(player.position, monsters[i].transform.position) <= strikingDist)
             {
                 monsters[i].Attack();
             }
         }
     }
 }

You will obviously want to separate the attacks, but this is just a quick and dirty example

avatar image Nickshepp · Mar 10, 2021 at 05:17 PM 0
Share

This is for a card game im writing which is a highly simplified version of slay the spire. I have the deck side sorted out, but presently all the monsters attack for a fixed amount on the enemy turn. I can make this work by using conditional logic in the "monster attack" phase of the code, checking the name and having specific attack logic for each type, but it produces heaps of code. I am looking for a more efficient way of declaring how the monster attacks, instead of (not actual code of course):
attacknumber=0;

if (monster="alpha")

{
if attacknumber=0

attack=1

if attacknumber=1

attack=2

if attacknumber=2

attack=random range (0,4)

"Do the Attack for 'attack amount' '"

attacknumber++

if attacknumber=3 then attacknumber=0

}

avatar image Nickshepp Nickshepp · Mar 10, 2021 at 05:27 PM 0
Share

The "monster" is just a static image with a sprite. It's all being done using the UI layer. I have a variable which deter$$anonymous$$es the monster damage. Currently its just a static hard coded value. As in my initial question I can load the monster image, name and damage from three arrays and update the display and the attack value,,, but I can only attack for one specific amount per monster. Thanks for helping.

avatar image highpockets Nickshepp · Mar 10, 2021 at 05:35 PM 1
Share

did you check my code above.. If you need the image or anything else to be related to the monster, just add those properties to the monster class and define it in the editor or make a scriptable object with each one. My code is just an example to get across the idea that you should have each monster defined as an instance of a class with damage amounts, images, names, etc.. then every time you access each monster, you will have access to all of that monster's properties that you defined specifically for that monster

avatar image Nickshepp · Mar 10, 2021 at 05:55 PM 0
Share

(Scales fall from eyes) Ah I see, sorry I was thrown by the vector 3 related stuff and couldn't see how that applied. Went code blind I think. Thats all really helpful. I'll admit that the whole classes thing is something I definately need to find out more on, but I get the core idea now I've read your code example properly and your last comment was incredibly helpful. Thank you very much, I know what I need to read up on (I've been learning Unity fairly randomly which has taken me so far, but I think its time to go a bit more back to "basics").

for (var i = 0; i < 3; i++)

{

Debug.Log("Thank you highpockets")

}

avatar image highpockets Nickshepp · Mar 10, 2021 at 05:59 PM 0
Share

@Nickshepp I keep converting your answers to comments. Just comment when you are not answering the question and it is standard when you mark the answer as "correct" if you feel it answered your question

avatar image Nickshepp highpockets · Mar 10, 2021 at 06:05 PM 0
Share

Yeah, sorry about that, I did convert my last "answer" to a comment after I posted it. This was my first post, and I don't find the interface that intuitive, but like my Unity skills I will try to improve. Thanks again and I will attempt to accept your answer...

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

118 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Null Ref Exception on a script that should have a reference in it? 2 Answers

How Do I Convert String to String Array In List.Add? 1 Answer

Using similar to GameObject.FindObjectsWithTag but for tag.contains. 0 Answers

How do I create and loop through a completely generic list in javascript? 1 Answer

How to add to list if not in list 3 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