Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Create Custom Variables on an Object?

Discussion in 'Scripting' started by B-30, Aug 2, 2019.

  1. B-30

    B-30

    Joined:
    Nov 3, 2014
    Posts:
    28
    Is it possible for a variable to have a custom variable, e.g., I do a foreach on a collection of AudioSources, I want to declare a variable to each AudioSource so I can manipulate it later.

    Like this (doesn't work, illustration only):

    Code (CSharp):
    1. foreach (AudioSource aSrc in allAudioSources) {
    2. bool aSrc.firstTime = true;
    3. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    Probably the easiest way is to create your own object that contains two things: and AudioSource and your .firstTime boolean. In your Start() function you'd make an array of your own object types and stuff the AudioSources in, then always iterate that list of your type of object.
     
  3. B-30

    B-30

    Joined:
    Nov 3, 2014
    Posts:
    28
    That makes sense, thanks Kurt!
     
    Kurt-Dekker likes this.