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

Assigning to Dictionary not possible

Discussion in 'Scripting' started by KaiserAfini, Feb 8, 2015.

  1. KaiserAfini

    KaiserAfini

    Joined:
    Aug 6, 2013
    Posts:
    7
    I have a Dictionary called tabs of MenuTab (custom class) indexed by the tab's name ( a String). Inside the tab, I have a dictionary called buttons of MenuButton (custom class), indexed by the id of the scene the button should load (int). However, when I try to modify the data inside the button like this:

    tabs[currentTab].buttons[sceneID].puzzleState = PuzzleState.Puzzle_Completed;
    tabs[currentTab].buttons[sceneID].button.GetComponent(Image).sprite = tabs[currentTab].buttons [sceneID].completedTexture;

    The compiler tells me I am doing an assignment to a temporary, sure enough, the original data remains unchanged. This makes little sense to me , since that is the correct syntax for assignment in C++ maps, which is very similar to a Dictionary. What would be the correct syntax for assignment ? Does the dictionary store a reference to my custom class or copies it by value ? Is there a way to enforce the storing of a reference ?

    Thank you in advance and have an excellent day

    PS: How do you mark a part of your post as code ? This is my first time posting here and I didn't see the option
     
  2. Zerot

    Zerot

    Joined:
    Jul 13, 2011
    Posts:
    135
    Are you using a struct instead of a class for your MenuButton? They are 2 different things in c#. Structs are valuetypes, but classes are referencetypes. You either need to change it to a class or use a temp var and assign that back to the dictionary. e.g.

    Code (csharp):
    1. var temp = tabs[currentTab].buttons[sceneID];
    2. temp.puzzleState = PuzzleState.Puzzle_Completed;
    3. temp.button.GetComponent(Image).sprite = tabs[currentTab].buttons;
    4. tabs[currentTab].buttons[sceneID] = temp;
    To display code on the forum, use the "code" tag. [.code=csharp][./code] without the .
     
  3. KaiserAfini

    KaiserAfini

    Joined:
    Aug 6, 2013
    Posts:
    7
    I am actually using the .NET data structures in JavaScript, and the types are classes that extend System.ValueType
     
  4. Zerot

    Zerot

    Joined:
    Jul 13, 2011
    Posts:
    135
    If they extend System.ValueType then they are not classes, but structs. Which is exactly the problem you are having, because valuetypes are copied by value, not referenced.
     
  5. KaiserAfini

    KaiserAfini

    Joined:
    Aug 6, 2013
    Posts:
    7
    So should I make them normal classes then ?
     
  6. KaiserAfini

    KaiserAfini

    Joined:
    Aug 6, 2013
    Posts:
    7
    I tried removing the extends and using normal types, yet the problem persists