Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DoctorMoney · Jul 26, 2014 at 06:08 PM · pickup

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndyMartin458 · Jul 26, 2014 at 08:16 PM 0
Share

@Doctor$$anonymous$$oney well the first guess would be to rename any object such as item0001 and item0002 to avoid any confusion.

avatar image EDevJogos · Jul 26, 2014 at 08:33 PM 0
Share

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.

avatar image DoctorMoney · Jul 26, 2014 at 09:59 PM 0
Share

@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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges