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 HiTryx · Sep 30, 2014 at 04:51 AM · 2draycasting

Differentiating between seperate game objects in code when one script is attached to multiple game objects? (2D)

I am trying to make it to where selecting one crate gives you one item and selecting another crate gives you a different item. I am using a single script to handle all of this. I am attempting to tag game objects and using the CompareTag() function to attempt to differentiate between two crates. Here is the script I am currently using:

 using UnityEngine;
 using System.Collections;
 
 public class CrateOpen : MonoBehaviour {
 
     public bool crateOpenBool = false;
 
     void FixedUpdate() { 
         GameObject player = GameObject.FindWithTag("Player");
 
         RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward);
 
         Vector2 pos = player.transform.position;
         float distance = Vector2.Distance(pos, hit.point);
 
         if (hit.collider != null && Input.GetMouseButtonDown(1) && distance <= 2.0) {
             // Area I am working with
             if (CompareTag("pCrate")) {
                 Debug.Log ("pistol");
             }
 
             if (CompareTag("kCrate")) {
                 Debug.Log ("knife");
             }
             // Area I am working with
             crateOpenBool = true;
         } else if (hit.collider == null || distance > 2.0) {
             crateOpenBool = false;
         }
     }
 }
 
Comment
Add comment · Show 1
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 tanoshimi · Sep 30, 2014 at 05:36 AM 1
Share

What's your actual question?

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kiwasi · Sep 30, 2014 at 08:30 PM

You could make this work simply by changing the problem lines to

 if(hit.collider.tag == "pCrate");

However I feel obligated to point out a few other problems in your script.

  • You are getting input in FixedUpdate. This is generally a bad idea, as the Input class is modified during Update. Its possible to miss inputs this way

  • You are doing a raycast regardless of weather you use it or not. Raycasts are expensive, checking input is cheap. Only do the raycast if your input is present.

  • For ultimate versatility I would suggest having this script use SendMessage to tell the crate it is selected. Then have the crate take care of the instantiating. This will allow the script to be reused for selecting any object. It would also allow crates to be modified for a larger combination of results.

Edit: Just reread the question. You should only have a single copy of this script on one GameObject in the scene. Having a copy on every crate will just lead to inefficiencies and problems.

Comment
Add comment · Show 2 · 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
avatar image HiTryx · Oct 07, 2014 at 11:08 PM 0
Share

Sorry for the late response: but thanks! this works fine. But, if possible, would you $$anonymous$$d going into a bit more detail about how I could use Send$$anonymous$$essage in this situation? Thanks!

avatar image Kiwasi · Oct 08, 2014 at 03:52 AM 0
Share

Sure. Some pseudo code to demonstrate the idea

Here is an input class. Attach it to the main camera. Or to a separate input manager. Or to the player. It really doesn't matter.

 public class Input$$anonymous$$anager : $$anonymous$$onoBehaviour {
     void Update (){
         if(Input.Get$$anonymous$$ouseButtonDown(1)){
             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward);
             if(hit.collider){
                 hit.collider.Send$$anonymous$$essage("Clicked",Send$$anonymous$$essageOptions.DontRequireReciever);
             }
         }
     }
 }

Here is the crate class. Attach to a crate. You can set the item to whatever you like via the inspector

 public class Crate : $$anonymous$$onoBehaviour {
     public GameObject item;
 
     public void Clicked (){
         Instantiate (item, transform.position, transform.rotation);
         Destroy(gameObject);
     }
 }

Where this really shines is when you want other things that can be clicked. Lets say your pistol. (Actually this would be better done in an item base class, or in an interface, but you get the idea.)

 public class Pistol : $$anonymous$$onoBehavior {
     public void Clicked (){
         AddPistolToInventory ();
         Destroy(gameObject);
     }
 }

And how about a door?

 public class Door: $$anonymous$$onoBehavior {
     private bool isOpen = false;
 
     public void Clicked (){
         isOpen = !isOpen;
     }
 }

And so forth. Essentially you are using one script to process your click and forward it to the right item. The item itself can then take care of the needed behaviour.

Send$$anonymous$$essage is more expensive then directly calling the method, as Unity has to go through all of the components of the GameObject to find your method. But for an input manager as you have described the ease of coding is worth it.

avatar image
0

Answer by LSPressWorks · Sep 30, 2014 at 05:20 AM

I know this isn't the specific answer you are looking for, but if you simply create a string var in the script, you can change it on any object. This means any crate can have any itemIdentifier provided either at runtime or via the inspector.

ie(warning this is pseudo code, probably won't run without a little work.)

 //------FROM AN OUTSIDE CLASS
 import crateClass;
 var caseToOpen : crateClass;
 var itemRet : String;
  OnCollisionEnter(thingy : collider)//or hits from a cast or whatever identifies the object to yank the function from. I think you used casts collider there. Either way.
  {
   //-- grab value from crate
   caseToOpen = thingy.GetComponent("crateClass");
   itemRet = caseToOpen.getItemTag();
   }

  //---------FROM WITHIN crate class
 var itemtag : String;//--for later use it's wise to create an array/vector whatever
 var wasPicked : boolean;
 
 //etc etc etc
 
 public function getItemTag() string
 {
 if(waspicked != true)
  {
 return this.ItemTag;
 waspicked = true;
  }else{return "Nada';}
 }
   
   

at which point you assign at runtime or via the edi

From what you are describing you'd want. Otherwise you have the right idea, it's only the part you're grabbing the info from that may be causing you issues.

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

Answer by Tepei · Sep 30, 2014 at 07:23 PM

Maybe create a central script that have a static array.

inside the script you want to tag, on start function add a new value in the array.. CentralScript.number.Add(x); Then get the lenght of this array and you have a different number for all the same scripts..
numb =CentralScript.number.Lenght;

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

Switching Cameras at runtime 1 Answer

Increase Jump Through Mouse Clicks? 1 Answer

2D Game - Y axis rotation (left - right) 2 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