I have written a simple script for picking up objects to add to an inventory. When the player looks at an gameobject, a message should appear saying “E to pickup”. So far this script works for objects with the ‘wood’ tag, but not other objects. Here is the code (javascript):
function Update()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "banana")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
inventory.banana++;
Destroy(hit.collider.gameObject);
guiShow = false;
}
}
if(hit.collider.gameObject.tag == "coconut")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
inventory.coconut++;
Destroy(hit.collider.gameObject);
guiShow = false;
}
}
if(hit.collider.gameObject.tag == "wood")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
inventory.wood++;
Destroy(hit.collider.gameObject);
guiShow = false;
}
}
else
{
guiShow = false;
}
}
}
function OnGUI()
{
if(guiShow == true)
{
GUI.Box(Rect(Screen.width / 2 - 150, Screen.height / 2 - 150, 100, 20), "E to pickup");
}
}
I have configured the appropriate tags for the other objects, but no message displays on screen as it does for objects marked with the ‘wood’ tag. Does anybody have any idea on what I’m doing wrong? I’ve been stuck on this for hours.