0

I have this ingredients array which contains multiple variables (like name, icon, value etc.). When the player has surpassed certain points a new ingredient has to be added to that ingredients array. So I have an other array which contains icons and names for the new ingredients (values are to be added separately at runtime).

But the thing is you just can't use Add to add new elements to the ingredients array (also tried to give both arrays the same variables), or just add an new element at the end and fill the elements separately.

Script for unlocking ingredients:

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

public class progressManager : MonoBehaviour {
    
    private int ingUnlockLevel;
    private int lockLevel = 0;
    private int newIngScore = 500;
    public List<ingUnlocks> ingUnlock = new List<ingUnlocks>();
    public List<GameObject> lockedCraft = new List<GameObject>();
    public Texture locks;
    public Texture normal;
    private bool upGrade;
    
    public TextMesh nextIngredient;
    // Update is called once per frame
    void Update () {
        
        nextIngredient.text = ingUnlock[lockLevel].name;
        
        ingUnlockLevel = GetComponent<gameMechanics>().headScore;
        
        if(ingUnlockLevel >= newIngScore){upGrade = true;}
        
        if(upGrade == true){    
            GetComponent<productManager>().ingredient.;
            newIngScore = newIngScore *2;
            lockLevel+=1; 
            upGrade = false;
        }
    }
    
    [System.Serializable]
    public class ingUnlocks{
        public string name;
        public Texture icon;
    }
}

And part of the script for the players ingredients:

using UnityEngine;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class productManager : MonoBehaviour {

    private class ingredientComparer : IComparer<ingredients>{
        
        public int Compare(ingredients a, ingredients b)
        {
            return ((int) a.c2cPerc ) - ((int) b.c2cPerc);
        }
    }

        public List<ingredients> ingredient = new List<ingredients>();
}

[System.Serializable]
public class ingredients{
    
    public string name;
    public Texture icon;
    public int score;
    public int quantity;
    public float c2cPerc;
    public bool usable;
    
}

Is there any way to add a new name and icon from the unlocking array, to the players ingredient array?

4
  • "But the thing is you just can't use Add to add new elements to the ingredients array (also tried to give both arrays the same variables), or just add an new element at the end and fill the elements separately." Why not? Commented Mar 28, 2012 at 13:51
  • 2
    Array's are immutable, if you want a data structure that grows, use a List. Commented Mar 28, 2012 at 13:52
  • So, you are saying ingUnlock.Add(new ingUnlocks()); does not work Commented Mar 28, 2012 at 13:53
  • 1
    @MrFox but the sample code shows a list, not an array. Kevin, your sample code does not declare a single array. I am therefore confused by your question. Commented Mar 28, 2012 at 13:56

1 Answer 1

1

it's hard to tell what you're after exactly, but does this answer your question?:

GetComponent<productManager>().ingredient.Add(new ingredients { name = ingUnlock[locklevel].name, icon = ingUnlock[locklevel].icon });

PS: I just completed the line of code from your sample that wouldn't compile and added a new ingredients item there, guessing this is what you wanted to do.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! That worked like a charm. I didn't know how to add multiple references to a variable array, but now I know! Thank you so much! Thank you all for your help and 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.