- Home /
Best way to add item to other script list?
Here my script named Boxitem:
import System.Collections.Generic;
var items : Createditems = new Createditems ();
var BoxList : List.<Items> = new List.<Items>();
function Start () {
BoxList.Add(items.AntirocketItem);
BoxList.Add(items.WaterItem);
BoxList.Add(items.AntirocketItem);
}
var Boxopen : boolean;
function OnMouseDown () {
Boxopen = !Boxopen;
}
function OnGUI () {
var w = 250;
var h = 300;
var rect = Rect((Screen.width-w)/2, (Screen.height-h)/2, 300, 200);
if(Boxopen){
GUI.Box (rect , "Box Items");
for(var x = 0; x < BoxList.Count; x++) {
if(GUI.Button(Rect(((Screen.width-w)/2) + 5, (((Screen.height-h)/2) + 30) + (30 * x), 290, 30), "Name: " + BoxList[x].Name + " Qty: " + BoxList[x].Qty)) {
Debug.Log("Item to add: " + BoxList[x].Itemsname);
var additem : ExtractorGUI = gameObject.GetComponent(ExtractorGUI);
additem.Itemtoadd = BoxList[x].Itemsname; //here my problem
additem.AddItem(); //not work too...
BoxList.RemoveAt(x);
}
}
}
}
function Update () {
if(BoxList.Count <= 0){
Destroy (gameObject, 1);
}
}
I create another script for vehicle ExtractorGUI who have his own inventory. and i
Sorry my message was cut...
Here ExtractorGUI script
import System.Collections.Generic;
var items : Createditems = new Createditems ();
var InvList : List.<Items> = new List.<Items>();
var EquipList : List.<Items> = new List.<Items>();
var Itemtoadd : String;
function AddItem () {
InvList.Add(items.Itemtoadd);
}
Witch way would be the best to add BoxList Items to InvList Items?
What is the ExtractorGUI "Itemtoadd" or "AddItem" method/code? What is the error
Thank for your quick answer!!
I use something like : InvList.Add(items.WaterItem); To add items to InvList..
The error is:
NullReferenceException: Object reference not set to an instance of an object BoxItem.OnGUI () (at Assets/Game building/BoxItem/BoxItem.js:25)
I'm not sure what the best way to acheive this task i try many think but the same error appear.
Your AddItem() function doesn't have an inbound parameter, you need to pass it something so it knows what to add
Thank!! i've modified my ExtractorGUI script to:
import System.Collections.Generic;
var items : Createditems = new Createditems ();
var InvList : List.<Items> = new List.<Items>();
var EquipList : List.<Items> = new List.<Items>();
function AddItem (Itemtoadd) {
InvList.Add(Itemtoadd);
}
So if i call it on ExtractorGUI it's work very well:
function Start () {
AddItem(items.WaterItems);
}
But i still unable to call the fonction on Boxitem
for(var x = 0; x < BoxList.Count; x++) {
if(GUI.Button(Rect(((Screen.width-w)/2) + 5, (((Screen.height-h)/2) + 30) + (30 * x), 290, 30), "Name: " + BoxList[x].Name + " Qty: " + BoxList[x].Qty)) {
var additem : ExtractorGUI = gameObject.GetComponent(ExtractorGUI);
var transferitem = BoxList[x];
Debug.Log("Item to add: " + transferitem);
additem.AddItem(items.transferitem);
BoxList.RemoveAt(x);
}
}
Think i'm really noob! Sorry...
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Master Server Current Games List 1 Answer
The name 'List' does not denote a valid type ('not found')? 1 Answer
Best way to list all active quests in a GUI 1 Answer
Custom list-item name 0 Answers