- Home /
AddComponent Question
Here is the situation, I have a script that I use to define my items:
public GameObject bigDBlock; //short for bigdefaultblock or cube
public static Item bigDCube;
void Start () {
defineItems();
}
public class Item {
public string name = "Empty";
public GameObject itemObject;
public float salt; //short for spawn altitude
public void OnLeftClick(Vector3 mypoint) {
itemObject.AddComponent<ItemID> ().ThisItem = this;
print ("Item name:" + itemObject.GetComponent<ItemID>().ThisItem.name);//Here
mypoint.y += salt;
Instantiate(itemObject, mypoint, Quaternion.identity);
}
}
void defineItems() {
bigDCube = new Item ();
bigDCube.itemObject = bigDBlock;
bigDCube.salt = 2f;
bigDCube.name = "BigDCube";
}
But when I call OnLeftClick, it throws the error NullReferenceException on the line that has //Here at the end. It seems to me that the statement above that line is the problem but it compiles fine. Is there something wrong with the way I call AddComponent?
Can anyone maybe give a reason why, as I have been fiddling with it for the longest time and I feel like things like this are really simple and I am looking at the wrong thing.
Additionally, The way I use this is I have an array of items that I use as the player's inventory and when the player right clicks it calls playerInventory[currentItemSlot].OnLeftClick(pointIamlookingat); to do whatever the left click action is for that item. There are currently 4 items, but they are very similar in format so I included just one for ease of reading. when the game calls Start and then defineItems, it defines the items in a way the rest of the game can use, by copying the pre-existing default items
Oh yes, here is the ItemID class as well
using UnityEngine;
using System.Collections;
public class ItemID : MonoBehaviour {
public ItemMan.Item ThisItem;
}
The way this is supposed to work is the player has their array of Items in their inventory and when they call the leftclick action for the currently held item (which would be the block above) it is supposed to copy the gameobject from inside the Item into the actual game, attach that ItemID script to it then set the inventory slot to an empty object. the opposite is supposed to occur when the player picks up an item: the gameobject.ItemID.ThisItem is copied into the player's inventory (space permitting) then the gameobject is destroyed
Answer by rutter · Aug 18, 2014 at 07:55 PM
It's important to remember that GetComponent returns the first matching component. If you have multiple such components attached, there's no guarantee that GetComponent will ever return the very same object that you just attached with AddComponent.
Instead, you should probably use the return value from AddComponent:
MyComponentType myComponent = someGameObject.AddComponent<MyComponentType>();
If you intend to use it repeatedly, that component could be stored in a lookup table, perhaps a List or Dictionary, or some other collection. That way, you can be more specific about which object references which particular component, and you can avoid unnecessary GetComponent calls.
hehe, yes well, I was looking around and found something that said something about Getcomponents[] returning all components of that type in the order they are in in the editor... In investigating that, I found that part of this was essentially attached HUNDREDS of the ItemID script to the 4 prefabs I currently have... I feel like that might have been interfering with the whole process of "getting the right value"
Also, as it is set up, the GetComponent call should only have to be made once (assu$$anonymous$$g the player has inventory space), it isnt made every frame or update.
Bwahahaha, It works! A couple more modifications and the general inventory system will be CO$$anonymous$$PLETE! Thanks!