Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Reddwarff · Jul 27, 2021 at 03:39 PM · raycastcubeeventsysteminteractionrepeating

Interacting with one objects activates other objects with similar scripts.

Some context here is that I'm trying to allow the player to interact with an object using an Event System and Raycast. About three different colored cubes are present within the scene and I want each individual cube to return its color in the console when the player interacts with them. The problem is when the player interacts with one cube and gets their color, they also get the color from the other cubes with similar scripts as well as repeats them. I know this is due to each cube having the same script, but I don't know how each script can be separated so they all don't print their color at once. Here is my cube script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class cubeScript : MonoBehaviour
 {
     public string color;
     void Start()
     {
         eventHandler.current.onObjectInteract += onCubeInteract;
     }
     void onCubeInteract() {
         switch(color) {
 
         case "Blue":
             Debug.Log("The color is blue.");
             break;
 
         case "Red":
             Debug.Log("The color is red.");
             break;
 
         case "White":
             Debug.Log("The color is white.");
             break;
 
         default:
             Debug.Log("No color.");
             break;
         }
     }
 }

Here is my Raycast camera script:

 using System.Collections;
 using System.Collections.Generic;
 using System;
 using UnityEngine;
 
 public class playerInteract : MonoBehaviour
 {
     private Camera camOrigin;
     public float rayDist = 100.0f;
 
     void Start()
     {
         camOrigin = Camera.main;
     }
 
     void Update() {
     RaycastHit hit;
         if (Input.GetKey(KeyCode.E)) {
             if (Physics.Raycast(camOrigin.transform.position, camOrigin.transform.forward, out hit, rayDist)) {
                 if (hit.transform.tag == "Interactable") {
                     eventHandler.current.objectInteracted();
                 }
             }
         }
     }
 }

And here is my Event Handler script:

 using System.Collections;
 using System;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class eventHandler : MonoBehaviour
 {
     public static eventHandler current; // Declare event handler
     void Awake()
     {
         current = this; // Assign current.
     }
 
     // Where all events are declared.
     public event Action onObjectInteract;
 
     // Where all events are invoked.
     public void objectInteracted() { 
         if (onObjectInteract != null) { // Check if event is null before invoking it.
             onObjectInteract();
         }
     }
 }

Thank you in advance, for taking the time to read.

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
Best Answer

Answer by Hellium · Jul 27, 2021 at 04:33 PM

Why making things so complicated??

 public interface IInteractable
 {
     void Interact();
 }
 
 // ----
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CubeScript : MonoBehaviour, IInteractable
 {
     public string color;
     public void Interact()
     {
         switch(color) {
 
         case "Blue":
             Debug.Log("The color is blue.");
             break;
 
         case "Red":
             Debug.Log("The color is red.");
             break;
 
         case "White":
             Debug.Log("The color is white.");
             break;
 
         default:
             Debug.Log("No color.");
             break;
         }
     }
 }
 
 // ----
 
  using System.Collections;
  using System.Collections.Generic;
  using System;
  using UnityEngine;
  
  public class PlayerInteract : MonoBehaviour
  {
      private Camera camOrigin;
      public float rayDist = 100.0f;
  
      void Start()
      {
          camOrigin = Camera.main;
      }
  
      void Update() {
      RaycastHit hit;
          if (Input.GetKey(KeyCode.E)) {
              if (Physics.Raycast(camOrigin.transform.position, camOrigin.transform.forward, out hit, rayDist)) {
                  if (hit.transform.TryGetComponent(out IInteractable interactable)) {
                      interactable.Interact();
                  }
              }
          }
      }
  }



But if you really want to go the complicated way:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CubeScript : MonoBehaviour
 {
     public string color;
     void Start()
     {
         EventHandler.current.onObjectInteract += OnCubeInteract;
     }
     void OnCubeInteract(GameObject go) {
         if(go != gameObject)
             return;
 
         switch(color) {
 
         case "Blue":
             Debug.Log("The color is blue.");
             break;
 
         case "Red":
             Debug.Log("The color is red.");
             break;
 
         case "White":
             Debug.Log("The color is white.");
             break;
 
         default:
             Debug.Log("No color.");
             break;
         }
     }
 }
 
 // ----
 
 using System.Collections;
 using System.Collections.Generic;
 using System;
 using UnityEngine;
 
 public class PlayerInteract : MonoBehaviour
 {
     private Camera camOrigin;
     public float rayDist = 100.0f;
 
     void Start()
     {
         camOrigin = Camera.main;
     }
 
     void Update() {
     RaycastHit hit;
         if (Input.GetKey(KeyCode.E)) {
             if (Physics.Raycast(camOrigin.transform.position, camOrigin.transform.forward, out hit, rayDist)) {
                 if (hit.transform.tag == "Interactable") {
                     EventHandler.current.objectInteracted(hit.transform.gameObject);
                 }
             }
         }
     }
 }
 
 // ----
 
 using System.Collections;
 using System;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EventHandler : MonoBehaviour
 {
     public static EventHandler current; // Declare event handler
     void Awake()
     {
         current = this; // Assign current.
     }
 
     // Where all events are declared.
     public event Action<GameObject> onObjectInteract;
 
     // Where all events are invoked.
     public void objectInteracted(GameObject go) { 
         if (onObjectInteract != null) { // Check if event is null before invoking it.
             onObjectInteract(go);
         }
     }
 }
Comment
Add comment · Show 1 · 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 Reddwarff · Jul 27, 2021 at 05:27 PM 0
Share

I saw, and I tested. And I got what I wanted. Thank you, kind stranger.

Things were so complicated because I didn't know what Interfaces were. I was trying to use an Event System for player interaction, and I found that it is better suited for other things. I am familiar with interfaces, and it turned that it was best for what I was trying to do.

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

221 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 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

Making UI block rays with touch inputs 0 Answers

OnPointerClick only triggers when clicked on the top part of UI Image 1 Answer

Looking for an efficient way to use Raycast for many objects in a scene 0 Answers

Can you raycast to detect a point on the other side of a cube 0 Answers

How do I add UI pop up with this script? 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