0

I have a problem that involves 2 different scripts. What i am trying to achieve is to use Strings to access a static variable like the title suggest.

My First Script:

using UnityEngine;
using System.Collections;

public class GameInformation : MonoBehaviour 
{
    //Upgrades Info, 1 is bought while 0 is not bought
    public static int TipJar;
}

My Second Script:

using UnityEngine;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class Upgrades : MonoBehaviour 
{

public List<PlayerTank> playerList;
public class PlayerTank // Here is my class for PlayerTank
{ 

    public string Name;
    public string Value;

    // This is a constructor to make an instance of my class. 
    public PlayerTank (string NewName, int NewValue) 
    {
        Name = NewName;
        Value = NewValue;
    }
}

void Start() 
{

    playerList = new List<PlayerTank>();

    playerList.Add (new PlayerTank("TipJar", GameInformation.TipJar));

    //To loop all the list
    for(int i = 0; i < playerList.Count; i++)
    {
        int TempInt = i;

        playerList[i].NewValue += 1; //This line works but Gameinformation.TipJar will still contain the value 0, i want it to be 1.

    }
}

}

My goal is to update the value in GameInformation.TipJar but it keeps containing 0.

I have tried to replace playerList[i].NewValue += 1; with GameInformation.playerList[i].Name(which is TipJar) += 1.

I have been searching for some time now and I could not find a solution, any ideas?

8
  • 1
    This question is not very clear, what are you trying to achieve? Do you have an expected output? Commented Sep 12, 2016 at 13:14
  • Sorry, but what are you trying to achieve? Is very unclear Commented Sep 12, 2016 at 13:16
  • your GameInformation class has only defined a int var "TipJar" Commented Sep 12, 2016 at 13:24
  • Sorry for the unclear question, my goal is just to make the value of GameInformation.TipJar to be 1 when inside the for loop(); Commented Sep 12, 2016 at 15:21
  • This doesn't work because when you add GameInformation.TipJar to the player, the value is copied, not referenced in memory. So when you change player value, new value is changed, not the static field. If you want to be able to change GameInformation.TipJar through player class, take a look at object boxing/unboxing to pass the integer as reference instead as value. Commented Sep 12, 2016 at 15:23

2 Answers 2

1

Most likely a typo?

Change

GameInformation.playerList[i].Name += 1; //This line will not work

to

playerList[i].Name += 1;

Also field Name is of type String, so i doubt that +1 makes sense here. I think you want something like

playerList[i].Name += (i + 1);
Sign up to request clarification or add additional context in comments.

Comments

0

Sorry for the unclear question, my goal is just to make the value of GameInformation.TipJar to be 1 when inside the for loop();

Taking this comment from you, I assume that GameInformation.TipJar is meant to be global (meaning it should be 1 when in your loop, regardless of the content of playerList). I'll provide you 2 ways of solving your problem under this assumption:

1. Use the static variable (the easy way)

If that is correct, then just set the static like GameInformation.TipJar = 1; right before the loop starts and then set it GameInformation.TipJar = 0; right after the loop.

2. Use reflection

If you have a reference to your GameInformation object (say it is named gio), you can set its fields by the field name as a string with: gio.GetType().GetField("TipJar").SetValue(gio, value). (see: https://msdn.microsoft.com/en-us/library/6z33zd7h(v=vs.110).aspx)


If that doesn't answer your question, then I am sorry and I really don't get what you are trying to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.