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

Using a string variable to access a member of a component

Discussion in 'Scripting' started by DaleTheWale, May 13, 2013.

  1. DaleTheWale

    DaleTheWale

    Joined:
    May 13, 2013
    Posts:
    3
    Hi guys,

    might be a simple solution, but I can not find it.

    How to acces a variable of another component by using a string as variable name?

    Like:

    Code (csharp):
    1.  
    2. var hComponent : myComponent
    3. var sVariable : String
    4.  
    5. hComponent = GameObject.FindWithTag("whatever").GetComponent(myComponent);
    6. hComponent.[sVariable] = 99;
    7.  
    8.  
    How to do this properly?

    Thanks!
     
  2. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    I am kind of confused at what you are trying to achieve :/
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    You can do it through reflection, but im not sure if its supported with JS. Assume it probably is..

    in c#, it would look something like this.. (after you get your reference to component)

    hComponent.GetType().GetProperty("PropertyName", BindingFlags.Instance).SetValue(hComponent, newValue);
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    Why do you want to do that? It implies either a terrible design or no design. Making any significant application like that is highly likely to be error prone and a maintenance nightmare.

    You already know the type of hComponent (it's defined as a myComponent) so if the variable is public you can access it directly. If it's not public there's probably a good reason for that.

    This kind of thing is also terrible for performance, as far as I'm aware. Doing it in once-off instances is fine, but it's something I'd keep out of my Update loop like plague.
     
  5. DaleTheWale

    DaleTheWale

    Joined:
    May 13, 2013
    Posts:
    3
    Ok, mybe there is a better way to do this.

    I want to use this for an inventory. In this case every object shares the same script. So when the player picks up any opject, I want to set variables related to this object, like the amount for instance.
    What other options do I have to avoid IFs and case switches?

    I use SendMessage now. Is this cheaper?

    Thanks!
     
    Last edited: May 20, 2013
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    How cheap it is depends entirely on how you're using it. For this kind of thing it really doesn't matter, though, unless for some reason you're hammering it all the time (ie: every frame).

    You have plenty of options to avoid ifs and switch statements. The best approach really depends on the details of what you're trying to do. There are virtually infinite ways you could implement an inventory, and different desired behaviours mean different approaches will suit better.

    Typically, the trick to getting nice code for this type of thing is to design an effective data structure and set of classes up front which suit what you'll need to be doing. How can you most elegantly represent the type of behaviour and data you want in your inventory system? Is your inventory a list like Skyrim? Is it a slot system like Baldurs Gate? Does weight matter? What kind of data do you need to keep about items? How do you want to be able to add/remove/use items?

    Don't just dive in and start writing if statements. They aren't your only tool, so don't use them exclusively to try to solve every problem. I'd suggest checking out how different data structures work (List<> and Dictionary<> might come in handy), and also how you can use custom classes to make life easier.

    For instance, perhaps you can have a class "InventoryItem" which contains the name, description, count, icon, etc. for a particular item, and your inventory can be a List<InventoryItem> with a number of helper functions? Mind you, that's just the first simple solution that jumps to the top of me head, and I don't actually know what you're trying to do - so it might not be a help at all.
     
  7. DaleTheWale

    DaleTheWale

    Joined:
    May 13, 2013
    Posts:
    3
    Thanks angrypenguin. I changed my setup from floats to hashtables for this which seems more elegant and works like I want it. Unfortunately I can't directly manipulate these tables with the regular inspector to "cheat" during developement.

    I'm starting over with my game I began with shiva3d, which works differently in some areas. (because I'm afraid that it's a dead end and I already know the unity editro pretty well from some previous projects)

    Thanks for pushing me in the right direction.
     
  8. AstralProjection

    AstralProjection

    Joined:
    Feb 12, 2013
    Posts:
    11
    For Items I keep a read-only master list of the Item Archetypes. Any Item instance that is spawned in-game gets a reference to its item archetype. Pretty simple and works.
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    You can always make a custom inspector for it if that's important, or a developer GUI in-game.
     
  10. OceanBlue

    OceanBlue

    Joined:
    May 2, 2013
    Posts:
    251