Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Giantbean · Aug 20, 2021 at 06:49 PM · arrayeventstype

Match strings from an array to fire specific UnityEvents?

I am trying to find/create a system that will allow different things to happen on a single object at different times. That's a simplification but its the basic idea.

I want to be able to compare strings and fire specific Unity Events when the string matches.

I have looked at generic Unity events and creating a type so I can have an array of strings and events but the array isn't even showing up in the inspector for me to start testing.

Here is the pseudo code I have started with: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events;

 public class ObjectEventString
 {
     public string cardType;
     public UnityEvent objectEvents;
 
     public ObjectEventString()
     {
 
     }
 }
 
 public class ObjectInteractionController : MonoBehaviour
 {
     //This aray is not showing up in the inspector?
     public ObjectEventString[] variations = new ObjectEventString[2]; 
 
     void Start()
     {
         
     }
 
     public void ObjectEventAction()
     {
         // for each matching string from the ObjectEventString class cardType and the server cardType trigger an event.
 
         //Something like:
         //check the string in the inspector against strings being passed by the server
 /*         foreach(string cardID in variations)
         {
             //when the string matches 
             if(variations.cardType == server cardType from server)
             //play the desired events and ignore the others.
                 cardID.variations.objectEvents.Invoke();
         } */
     }   
 }

Thank you.

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

2 Replies

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

Answer by Giantbean · Sep 15, 2021 at 02:17 PM

This does what I want

         private string owningType = "";
         [Tooltip("String Array this interactable could apply to")]
         public string[] interactionType;
         [Tooltip("Array of events that will fire when matched at index with a string from above array upon interaction from player")]
         public UnityEvent[] interactableInteractedWithEvent;
         [Tooltip("Default event fired upon interaction from player")]
         public UnityEvent defaultInteractedWithEvent;
 
 
         void Start()
         {
             //left start so I can disable the script in the inspector.   
         }
 
         public void CheckForInteractedEvent()
         {
             Debug.Log("CHECK FOR INTERACTED EVENT");
             // loop through the strings of cardTypes i.e. "criticalComputerUpdate"
             for (int i = 0; i < cardType.Length; i++)
             {
                 // if it matches get the index and pass that index into the array of events to fire the event we tied to it
                 if(owningType == cardType[i])
                 {
                     Debug.Log("Fire the associated INTERACTED Event!");
                     interactableInteractedWithEvent[i].Invoke();
                 }
                 else
                 {
                     // do default...
                     defaultInteractedWithEvent.Invoke();
                 }
             }
         }
 
         public void CheckForInteractedCompletedEvent()
         {
             Debug.Log("CHECK FOR INTERACTED COMPLETED EVENT");
             defaultInteractedWithEvent.Invoke();
             owningType = "";
 
         }
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
avatar image
0

Answer by qsp18 · Aug 20, 2021 at 07:06 PM

Make a dictionary. The Key is a string, the Value is an Action

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 Giantbean · Aug 20, 2021 at 09:34 PM 0
Share

Sorry newb here, I have not used dictionary's and while I have been looking at them the past few days I don't understand how the Value can be a UnityEvent that I can set in the inspector to do different things at different times. I would like to put this script on as many as 150 objects in a scene and each object could have between 1 and 6 different events that can happen based on the string that is compared. can the dictionary populate an array of unity events in the inspector?

avatar image Giantbean · Aug 30, 2021 at 03:59 PM 0
Share

Well I have used a dictionary now and its still not doing what I want.

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

138 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 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

Question about FindObjectsOfType 1 Answer

Variable Type for an Instance of any Script 2 Answers

how would I make an array of different scripts 1 Answer

How to store TYPE variables names into an Array, and assign values to them. 0 Answers

Call array-specific methods on arrays within an array? 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