- Home /
The name 'gameObject' does not denote a valid type?
// LootableObject.js
// Make the GameObject "Lootable".
// This script will
// Attach this script to any GameObject that is "Lootable"
#pragma strict
private var theInventoryItem : InventoryItem; // The current contents of this lootable object
private var thisLootableItem : gameObject; // A reference to this instance of this script
private var clicked : boolean; // Trap for creating the current contents only once
private var canLoot : boolean; // Boolean to control TestDistance.
private var playerTransform : Transform; // A reference to the Player Transform
private var thisTransform : Transform;
var itemName : String;
var itemIcon: Texture2D;
private var Inventory;
function Start ()
{ // Setup the initial states of the variables
//itemName = the object name
itemName = this.gameObject.name;
//itemIcon will get selected in Unity
//itemDescription as well
var theInventoryItem = new InventoryItem();
//Init thhe inventory item
theInventoryItem.itemName = itemName;
theInventoryItem.itemIcon = itemIcon;
thisTransform = this.transform;
playerTransform = gameObject.FindWithTag("Player").transform;
thisLootableItem = this.gameObject;
clicked = false;
}
function OnMouseDown()
{
Loot ();
}
function Loot ()
{
if (!clicked)
{ // If not yet clicked, check Loot Table
clicked = true;
}
//Add the item to the inventory
Inventory.AddItem(theInventoryItem);
theInventoryItem = null; //Set it to null
//Destroy the game object
Destroy(this.gameObject);
}
This is a code for a GUI inventory. This is the code that goes on the items that you pick up in the game. It says The name 'gameObject does not denote a valid type ('not found'). When I click on the error it brings me to line 9. I`m new to scripting so this is a stupid question most likely.
Thanks!!!!!!
Answer by Joyrider · Aug 11, 2013 at 07:26 PM
line 9, you should put GameObject with a capital G
GameObject is the class
gameObject is the internal variable refering to the GameObject class object the script is attached to.
Thanks, I think that cleared up that problem, but now in line 55 it says 'Additem' is not a member of 'Object'.
arg, javascript and the autotyping >.< Hate that. $$anonymous$$y guess is your Inventory variable is a List of InventoryItems. Since it was neither explicitly declared nor initialized, Unity declared it as an object which does not have and AddItem member.
Not sure you have to initialize in javascript, but if you declared it as a List, maybe you'd avoid that error.
This is wrong:
private var Inventory;
Declare the type. (Also use lowercase for the variable name, which makes things less confusing.)
@Eric5h5 How do I declare the type? Again, I`m new to this and trying to learn, so I don`t really understand most of it yet.
to declare a type in javascript, do private var VariableName : Type