- Home /
Unity mistaking one object for another
It's hard to describe my problem without actually uploading my game file but I'll try. Basically I have a script that works PERFECTLY when I pick up a sword and an axe and vice versa and whatever. However, when I try copy and pasting either one and picking it up in game, Unity thinks that it's the object I copy and pasted from. So if I try picking up the original sword everything's fine, however if I pick up the copy (same name and everything, all variables are assigned properly) it sets the original to Vector3.zero in stead of the copy but still parents the copy correctly, just nothing else works.
var crosshair : GUITexture;
var player : GameObject;
var layermask : LayerMask;
var halo : GameObject;
var weapon_id : float;
var shield_id : float;
var torch : GameObject;
var right_arm_weapon : GameObject;
var left_arm_weapon : GameObject;
private var isShield : boolean;
function Start () {
if (weapon_id > 0){
isShield = false;
}
else
{
isShield = true;
}
}
function Update () {
var ray : Ray = Camera.main.ViewportPointToRay(crosshair.transform.position);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit, Mathf.Infinity, layermask))
{
if (hit.transform.name == gameObject.transform.name && Vector3.Distance(transform.position, player.transform.position) <= 3)
{
halo.GetComponent("Halo").enabled = true;
if (Input.GetKeyDown(KeyCode.E))
{
if (isShield == false){
//Drop Current Weapon
if(player.GetComponent("PlayerScript").weapon_id != 0){
player.GetComponent("PlayerScript").currentWeapon.transform.localScale = Vector3(1,1,1);
player.GetComponent("PlayerScript").currentWeapon.gameObject.layer = LayerMask.NameToLayer("Default");
player.GetComponent("PlayerScript").currentWeapon.gameObject.AddComponent("Rigidbody");
player.GetComponent("PlayerScript").currentWeapon.collider.isTrigger = false;
right_arm_weapon.transform.DetachChildren();
}
//Equip New Weapon
player.GetComponent("PlayerScript").weapon_id = weapon_id;
player.GetComponent("PlayerScript").currentWeapon = hit.transform.gameObject;
gameObject.transform.parent = right_arm_weapon.transform;
player.GetComponent("PlayerScript").SwitchWeapon();
}
else
{
//Drop Current Shield
if(player.GetComponent("PlayerScript").shield_id != 0){ //if the player has a shield
player.GetComponent("PlayerScript").currentShield.transform.localScale = Vector3(1,1,1); //returns shield to regular size
player.GetComponent("PlayerScript").currentShield.gameObject.layer = LayerMask.NameToLayer("Default"); //sets layer to default
player.GetComponent("PlayerScript").currentShield.gameObject.AddComponent("Rigidbody"); //adds the rigidbody
player.GetComponent("PlayerScript").currentShield.collider.isTrigger = false; //lets it collide with the world again
left_arm_weapon.transform.DetachChildren(); //detachs it from player
}
//Equip New Shield
player.GetComponent("PlayerScript").shield_id = shield_id; //sets shield id in other script
player.GetComponent("PlayerScript").currentShield = hit.transform.gameObject; //sets gameObject variable to what the player picked up
gameObject.transform.parent = player.GetComponent("PlayerScript").left_arm_weapon.transform; //parents this new shield to left_arm
player.GetComponent("PlayerScript").SwitchShield(); //calls function to switch shield
}
halo.particleSystem.enableEmission = false;
}
}
else
{
halo.GetComponent("Halo").enabled = false;
}
}
}
The shield part is the same as the weapon part, works fine but still has the problem with the copying thing.
(This is the switch weapon function in my other script)
function SwitchWeapon () {
if (weapon_id == 0){
right_arm.renderer.enabled = false;
currentWeapon.renderer.enabled = false;
}
if (weapon_id == 1){
right_arm.renderer.enabled = true;
right_arm.animation.Play("left_arm_up");
currentWeapon.collider.isTrigger = true;
currentWeapon.transform.localPosition = Vector3(0, 0, 0);
currentWeapon.transform.localRotation = Quaternion.Euler(270,0,0);
Destroy(currentWeapon.rigidbody);
currentWeapon.gameObject.layer = LayerMask.NameToLayer("Weapon");
}
if (weapon_id == 2){
right_arm.renderer.enabled = true;
right_arm.animation.Play("left_arm_up");
currentWeapon.collider.isTrigger = true;
currentWeapon.transform.localScale = Vector3(1.3,1.1,1.5);
currentWeapon.transform.localPosition = Vector3(0, 0, 0);
currentWeapon.transform.localRotation = Quaternion.Euler(270,346.2527,0);
Destroy(currentWeapon.rigidbody);
currentWeapon.gameObject.layer = LayerMask.NameToLayer("Weapon");
}
}
Everything worked perfectly except for when I try to copy it. I even have a halo set up on the objects so when I hover over them with my cursor they light up to indicate that you're looking at it but when I copy it over and change the halo variable to the correct one, it still lights up both when you're only looking at the one. It doesn't make sense to me, can anyone help? Thanks
@Doctor$$anonymous$$oney well the first guess would be to rename any object such as item0001 and item0002 to avoid any confusion.
I don't know if i understood it right, but my guess is that you not saving the objects somewhere, you only have one instance of the script running, so when you change one all change.
You could save each object on a data structure or assing to them a tag and look for the object with that tag before making any changes.
Sorry for my english, google translate style.
@Andy$$anonymous$$artin458 wow... I really didn't think the answer would be that easy. I thought Unity was able to differentiate between two objects with the same name but I guess not when getting the name off a raycast. If you post it as answer i'll mark it as correct
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Very simple picking up items script? 2 Answers
I have an error placing my pickup script into game 0 Answers
Pick Up Weapons 1 Answer
Phisics question! How can I launch this object?!??!?!?!! 0 Answers