• 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 alexandersueme · Oct 03, 2020 at 07:40 PM · prefab-instance

How do I check to see if two gameObjects are of the same prefab?

I am trying to make a game where the player can hold one weapon in each hand and if the 2 weapons are the same weapon or spell, it would do...something. Right now I just want the program to actually recognize when the 2 weapons are of the same type, which is not happening. I think it's because the way I wrote it makes it so it's checking to see if the 2 weapons are literally the exact same gameObject.

 public Spell Rweapon;
 public Spell Lweapon;
 
 public void setweaponr(Spell weapon)
     {
         if (Rweapon != null)
         {
             Destroy(Rweapon.gameObject);
 
         }
 
         if (weapon == Lweapon)                ***this is the part that is not registering correctly***
         {
 
             Debug.Log("2hand");
         }
         else
         {
             Rweapon = Instantiate(weapon, gun1.position, gun1.rotation, gun1);
             Rmcost = Rweapon.manacost();
 
    
         }      
     }

As it is, it should be showing a message in the console that says 2HAND, but it's not showing up. Everything else works just fine but not that part.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityToMakeMoney · Oct 03, 2020 at 10:40 PM

You are right! It is checking if the two game objects are exactly the same.


How to fix this?

Method 1: The simplest solution would just to have a variable, like a string, and compare if they are the same.

Method 2: Similarly, you can also check the tags of the two game objects (weapons) and check if they have the same tag.


If you choose method 1, here some template code:

 /*within each weapon script*/
 
 private string weaponType = "typeOfWeapon";//assign the string
 
 /*Checks If The Parameter String Is Equivalent To 'weaponType'*/
 public bool isEqualType(string type){
      return weaponType.Equals(type);
 }
 
 /*Gets Weapon Type String*/
 public string GetWeaponType(){
      return weaponType;
 }
 
 
 /*-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=*/
 /*within the script you provided above in your post*/
 
 //replace 'InterfaceScript' with the appropriate script name
 if(weapon.GetComponent<InterfaceScript>().isEqualType(Lweapon.GetComponent<InterfaceScript>().GetWeaponType())){//if they are the same type
      //then, whatever you want to do here
 }

If you choose method 2:

 if(weapon.tag.Equals(Lweapon.tag)){//if they have the same tag
       //then, whatever you want to do here
 }
 /* *
  * [IMPORTANT]: You must add tags to the game objects/weapons (done within the inspector)
  * */

Tutorial On Tags


If you need additional help, feel free to let me know. :) @alexandersueme

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

136 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Source Gameobject rendered when using prefab 0 Answers

Custom Inspector: Using SeralizedProperty changes the prefab values! 0 Answers

Instantiating prefabs creates a clone, not instance of the prefab 1 Answer

"Unable to instantiate prefab. Prefab may be broken" 7 Answers

prefab navagent doesnt work,prefab navmesh doesnt work 1 Answer


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