• 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 ArchfiendGaming · Aug 07, 2015 at 04:11 PM · javascriptarrayunity 4.6clones

Add items from array to a new clone and give them values.

Hey guys, I have a script called Perks and in the script is 3 seperate Arrays all currently strings. An example of these is

 //likely to start fights
 "Agressive",
 
 //likely to steal money
 "Thief",
 
 
 //Drug addict, likey to steal from clients, high upkeep
 "Drug Addict",
 
 //low upkeep
 "Workaholic"



I would like to make it so that when a clone is spawned one or more of these perks are added to it i already have MinPerks and MaxPerks variables attached to the clone and i'd want to spawn a random number of perks between the max and min and for them to be a random selection. I'd also like to give these strings values for example i'd like to check if the clone had the thief perk and if it did in crease the StealChance variable from 0.1 to say 0.6.

I realise I'm asking alot and if I haven't given enough info I appologise. Thanks in advance for any help you can offer

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

Answer by allenallenallen · Aug 08, 2015 at 08:08 AM

Well, let's see if I can do this.

I have a solution in my mind already but it's a bit difficult to explain all of this in text. Plus, I use C# so you have to convert, but the idea and design are the same.)

I made 3 scripts for all of this.

This is the class script for you to easily declare more perks.

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class EnemyPerks
 {
     public int id;          // So that even if you mess with the list, you can still find the same perk using its ID.
     public string name;     // Name of the perk.
     public float value;     // The perk's value to add.
 
     // Makes it easier for you to add perks.
     public EnemyPerks(int iden, string nam, float val)
     {
         id = iden;
         name = nam;
         value = val;
     }
 }

This script is where you can make more perks. Attach this script to a game manager game object.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Perks : MonoBehaviour {
 
     public static List<EnemyPerks> enemyPerks = new List<EnemyPerks>();
 
     // Add your own list of perks. It can be as long as you want.
     void Awake()
     {
         EnemyPerks p1 = new EnemyPerks(0, "Agressive", 0.3f);
         enemyPerks.Add(p1);
 
         EnemyPerks p2 = new EnemyPerks(1, "Thief", 0.1f);
         enemyPerks.Add(p2);
 
         EnemyPerks p3 = new EnemyPerks(2, "Drug Addict", 0.6f);
         enemyPerks.Add(p3);
 
         EnemyPerks p4 = new EnemyPerks(3, "Workaholic", 0.2f);
         enemyPerks.Add(p4);
 
     }
 
 }

This script is attached on your clone to create random perks. Attach this script to your clones.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Clone : MonoBehaviour {
 
     public List<EnemyPerks> myPerks = new List<EnemyPerks>();   // This clone's own perks.
 
     public int minPerks = 1;
     public int maxPerks = 3;
 
     // This list of perks all have 0 because this is the default state of this clone.
     // But you can easily change the values if you want a higher base perk value.
     void Awake()
     {
         EnemyPerks p1 = new EnemyPerks(0, "Agressive", 0f);
         myPerks.Add(p1);
 
         EnemyPerks p2 = new EnemyPerks(1, "Thief", 0f);
         myPerks.Add(p2);
 
         EnemyPerks p3 = new EnemyPerks(2, "Drug Addict", 0f);
         myPerks.Add(p3);
 
         EnemyPerks p4 = new EnemyPerks(3, "Workaholic", 0f);
         myPerks.Add(p4);
     }
 
     void Start () 
     {
         // Say you want a mininum of 1 and maximum of 3 perks to add to this clone.
         int numPerks = Random.Range(1, 3);
 
         // Tells you how many perks this clone should have increased in the concole.
         Debug.Log("Num of Perks to Add: " + numPerks);
 
         // Creates a temporary list just for counting and getting a random perk off of it.
         List<EnemyPerks> tempList = new List<EnemyPerks>(Perks.enemyPerks);
 
 
         for (int i = 0; i < numPerks; i++)
         {
             // Pick a random perk fromt the list.
             int randPerk = Random.Range(0, tempList.Count - 1);
 
             // Take that perk's value and add it to this clone's perk's value.
             myPerks[tempList[randPerk].id].value += tempList[randPerk].value;
 
             // Remove this perk from the list so that you won't randomly get this perk twice.
             tempList.RemoveAt(randPerk);
         }
 
     }
 
 
 }


The concept is quite simple but I made it easier to extend and user friendly.

Comment
Add comment · Show 2 · 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 ArchfiendGaming · Aug 09, 2015 at 05:39 PM 0
Share

Thanks, this has been really helpful, however after translating the code, I am getting an error at this line "$$anonymous$$yPerks[TempList[RandPerk].Id].Value += TempList[RandPerk].Value;" The error reads "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[GirlPerks].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) GirlClonePerks.Start () (at Assets/Scripts/$$anonymous$$iniGames/GirlClonePerks.js:48)" I've given it a google but can't seem to fix it

EDIT: i should add this is in the last script you wrote line 47 in your post

avatar image allenallenallen · Aug 10, 2015 at 03:30 AM 1
Share

It means the range of the array is shorter than our index can get. I'm sorry but you might have to debug that. Check for the variable randPerk and see if it goes over the index value. Or maybe you put too high of a number for numPerks and it's trying to get non-existing perks?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multidimensional array variable size (JS) 1 Answer

Array variable assigned to instantiated object changes in all instantiated objects? 1 Answer

Creating a 2d array with the Array class 1 Answer

Physics.OverlapSphere with a min radius? 1 Answer

Changing the light color with sliders 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