You’re actually destroying the item, not picking it. A simple form to pick the object using your script is to set a flag indicating it was picked, save its offset from the character position and disable its renderer - it makes the object invisible. When dropping the item somewhere, clear the flag, move the object there and enable its renderer:
private var isHighlighted : boolean = false;
private var gameCharacter : Transform; // I'm supposing this is the player's transform
private var distance : float;
private var picked : boolean = false; // flag to indicate the object was picked
private var offset : Vector3; // distance from the player
private var iName : GameObject; // a little improvement to your code
function OnMouseEnter() {
if (distance <= 4.0) {
iName = CreateInfoName(); // save the name created in iName
renderer.material.color = Color.red;
isHighlighted = true;
}
}
function OnMouseExit() {
renderer.material.color = Color.white;
if (iName) Destroy(iName); // destroy the name created
isHighlighted = false;
}
function Update () {
if (picked){ // if object picked check if it's being dropped
if (Input.GetKeyDown("r")){ // if dropping object
// move it to the present position
transform.position = gameCharacter.position + offset;
picked = false; // clear the flag
renderer.enabled = true; // and make it visible again
}
}
else // only pick an object if not already picked
if (Input.GetKeyDown("e") && isHighlighted == true){
renderer.enabled = false; // make the object invisible
picked = true; // signal it has been picked
// save the position relative to the player
offset = transform.position - gameCharacter.position;
}
}
function CreateInfoName():GameObject { // return object created
var infoName = new GameObject("infoName");
infoName.AddComponent(GUIText);
infoName.GetComponent(GUIText).text = gameObject.name;
infoName.transform.position = Vector3(0.5, 0.5, 0);
infoName.GetComponent(GUIText).alignment = TextAlignment.Center;
infoName.GetComponent(GUIText).anchor = TextAnchor.LowerCenter;
infoName.GetComponent(GUIText).pixelOffset = Vector2 (0, 25);
return infoName;
}
Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.UnauthorizedAccessException: Access to the path “C:\Users\jaime jones\Documents\Newtestproject\Temp\Assembly-CSharp.dll.mdb” is denied.