简体   繁体   中英

values not read from array

This is my first time using a 2D array and i cant seem to get a fix for this. i am working in unity and i have a button for each of the methods at the bottom of the codes. every time i press a button the debug shows that the method is called correctly and even adds it to the previously stored value correctly.

the problem is when i try to assign something to a value stored in the array it doesn't retrieve the right value. so in this case if i call add addenergy(); 2 times the debug would show

"added 1 to 0,02"

even though i set it to 3 in start. the checking value would still be 3 and tab would also show 3. what am i doing wrong?

using UnityEngine;
using System.Collections;

public class factioncontroller : MonoBehaviour {

public int[,] skills = new int[4, 3];

public int check;

// Use this for initialization
void Start () {

 skills[0, 0]=3;
}

// Update is called once per frame
void Update () {
    if (Input.GetKeyDown("tab")) { Debug.Log(skills[0, 0]);         
    check = skills[0, 0];
}
public void addskill(int x, int y) { skills[x, y] += 1; Debug.Log("added 1    to" + x + "," + y + skills[x, y]); }
public void addenergy() { addskill(0,0); }
public void addregen() { addskill(0, 1); }
public void addmovement() { addskill(0, 2); }
public void addusesS() { addskill(1, 0); }
public void addduration() { addskill(1, 1); }
public void addemp() { addskill(1, 2); }
public void addrangeD() { addskill(2, 0); }
public void adddistract() { addskill(2, 1); }
public void addstun() { addskill(2, 2); }
public void addrangeG() { addskill(3, 0); }
public void addusesG() { addskill(3, 1); }
public void addmobi() { addskill(3, 2); }
}

I don't know the order in which you are calling the methods, but if your intention is to initialize skills , then why not initializing it in a constructor?

Remove the Start() method, and add the following one:

// Use this for initialization
public factioncontroller()
{
    skills[0, 0] = 3;
}

This will be guaranteed to be called before you call any instance method on this class.

From what you are experiencing, it seems that Start() is not being called at all, or called after addenergy()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM