Hello!.
I’m relatively new to unity, not sooo new but a bit. I will post my problem in two parts, the long story and the short one:
LONG VERSION
I’m making a game where I can pick some items, that are defined by a ItemClass made in JS. The ItemClass stores the mass, the name, the type, the GameObject etc. The object have a script “when you pick me, i will give you my ItemClass data and i’ll destroy myself” and the player have an inventory script “my function Pickup will take that data and then will store it in my ItemClass list”. I have worked with all of that and everything was going ok until now: I want some items to be “placeables” and when I click, I want to instantiate them, but for some reason when i try to do that, i get an error “wtf, there is no object to instantiate” and after searching for two days i found the error, my “Pickup” function recieved the ItemClass data, all the variables, but stored all the variables in the inventory except one: the GameObject variable.
SHORT VERSION
I have an ItemClass that stores some variables that define items, and some objects that send the data to my player. The player have an Inventory that take that data with a pickup function and then stores it in an ItemClass list. The function takes properly all the variables of the ItemClass and stores almost all but the GameObject variable.
VERY SHORT VERSION
My pickup function don’t want to send the GameObject! >:(
The ItemClass.js script:
public class ItemClass
{
enum ItemType {None, Weapon, Material, Food, Wear, Building, Light, Other}
public var id : int;
public var name : String;
public var obj : GameObject;
public var type : ItemType;
public var icon : Texture2D;
public var mass : float;
public var force : int;
}
The Pickable.js script (the one that sends the ItemClass to the player)
var Pickable : boolean = true;
var itemInfo : ItemClass;
function OnTriggerStay (other : Collider)
{
if (other.gameObject.name == ("Player")) {
var Inventory : Inventory = other.GetComponent(Inventory);
if (Input.GetKey("e")) {
if (Inventory.isRecieving == true && Pickable == true) {
//Debug.Log("Sending " + itemInfo.name); works fine
//Debug.Log("Sending " + itemInfo.type); works fine
//Debug.Log("Sending " + itemInfo.obj); works fine
other.gameObject.SendMessage ("Pickup", itemInfo);
Destroy (transform.parent.gameObject);
} else {
Debug.Log("You can't Pickup this object");
}
}
}
}
The Inventory.js Script
import System.Collections.Generic;
var inventory : List.<ItemClass> = new List.<ItemClass>();
var isRecieving : boolean = true;
var inventoryOpen : boolean = false;
var tempObj : GameObject;
function Pickup (itemInfo : ItemClass) {
inventory.Add (itemInfo);
//Debug.Log("Recieved " + itemInfo.name); works fine
//Debug.Log("Recieved " + itemInfo.type); works fine
//Debug.Log("Recieved " + itemInfo.obj); works fine
var x : int = inventory.Count - 1;
tempObj = itemInfo.obj; //trying to set a temporal object
//Debug.Log("Saved " + inventory[x].name); works fine
//Debug.Log("Saved " + inventory[x].type); works fine
//Debug.Log("Saved " + inventory[x].obj); works fine
//inventory[x].obj = tempObj; //trying to set the temporal object to the ItemClass
Debug.Log("tempObj: " + tempObj); //this is to see if the temporal object is being saved
//and it works fine also, the tempObj is defined
}
function Update () {
if (Input.GetKeyDown("r")) {
if (inventoryOpen) {
inventoryOpen = false;
} else {
inventoryOpen = true;
}
}
Debug.Log("tempObj: " + tempObj); //here, after defining the variable in the Pickup function, it returns null. WTF!!!
//And in the Inventory this is what i get:
//inventory[x].name = works fine
//inventory[x].type = works fine
//inventory[x].obj = NULL!!! THIS IS DAMN NULL!!!
}
In the inspector in the itemClass list, the game object is “Missing (GameObject)”
I also tried with sending just the GameObject and then check in the Inventory.Update() if it worked, but always the same. Far as I know the problem is when the pickup function tries to send the data out of the function, like it don’t support sending gameObjects, but I have no Idea.
EDIT: I think this is important and i forgot to say it: the itemData.obj does not store the GameObject in wich the code is, it stores a Prefab in the project assets, so destroying the object is not suposed to destroy the itemData.obj too, i think.
Also, could be interesting information, in the Pickup function, when I set tempObj = gameObject (the player gameObject), it works fine and in the Update function it returns the GameObject properly. It seems like the error just happen with the ItemClass gameObject, it makes no sense.
I need some help please thanks in advance.
PD: sorry my english…