Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How to click two objects in script?

Discussion in 'Editor & General Support' started by clurpslurp, Jul 23, 2023.

  1. clurpslurp

    clurpslurp

    Joined:
    Jul 3, 2023
    Posts:
    23
    When a player clicks an object(bad chain), it will destroy itsself and then instantiate another prefab (broken chain). Then, the prefab has to be clicked to start a function(minigame).
    This script is like this.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEditor;
    5. using UnityEngine;
    6. using UnityEngine.Playables;
    7. using UnityEngine.SocialPlatforms.Impl;
    8.  
    9. public class ChainFixS : MonoBehaviour
    10. {
    11.     public Transform hand;
    12.     public GameObject badChain;
    13.     public GameObject brokenChain;
    14.     private GameObject brokenChainInstance;
    15.     private bool _badGone;
    16.  
    17.     //animation
    18.     public PlayableDirector playableDirector;
    19.  
    20.     //minigame
    21.     //canvases
    22.     private int score;
    23.  
    24.  
    25.     private void Update()
    26.     {
    27.         if (Input.GetMouseButtonDown(0) && _badGone == false)
    28.         {
    29.             if (GameObject.Find("GrinderTriggerPrefab(Clone)") != null)
    30.             {
    31.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    32.                 RaycastHit hit;
    33.                 if (Physics.Raycast(ray, out hit, 100f))
    34.                 {
    35.                     //visibility
    36.                     Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
    37.                     Debug.DrawRay(transform.position, forward, Color.green);
    38.                     if (hit.collider.transform.name == "Chain")
    39.                     {
    40.                         Debug.Log("Clicked");
    41.                         Destroy(badChain);
    42.                         _badGone = true;
    43.                         brokenChainInstance = Instantiate(brokenChain);
    44.                         StartCoroutine(animationWait());
    45.                         return;
    46.                     }
    47.                 }
    48.             }
    49.         }
    50.  
    51.         if (Input.GetMouseButtonDown(0) && _badGone==true)
    52.         {
    53.             Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
    54.             RaycastHit hit2;
    55.             //visibility
    56.             Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
    57.             Debug.DrawRay(transform.position, forward, Color.green);
    58.             if (Physics.Raycast(ray2, out hit2, 100f))
    59.             {
    60.                 if (hit2.collider.transform == brokenChainInstance)
    61.                 {
    62.                     Debug.Log("Clicked");
    63.                     StartCoroutine(Minigame());
    64.                     return;
    65.                 }
    66.             }
    67.         }
    68.  
    69.     }
    70.     IEnumerator animationWait()
    71.     {
    72.         playableDirector.Play();
    73.         yield return new WaitForSeconds(1);
    74.     }
    The scipt is attached to an empty that has all the parts as it's children
    Clicking the object works, but clicking the prefab doesn't work. Both objects have a rigidboy and a collider. From the Debug.DrawRay, the ray cast for the prefab inside in the right direction? Is the earlier ray cast interfering with it?
    I tried making raycasting a method and then applying it to the two different situations, but I'm not sure on how I could apply two different actions to the same method.
     
    Last edited: Jul 23, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,558
    "Doesn't work" is not useful.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?

    If you just have bugs, then...

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. clurpslurp

    clurpslurp

    Joined:
    Jul 3, 2023
    Posts:
    23
    Thanks,
    I tried to find some things so Ill try to focus this question.
    I'm not familiar with Raycasts, so I'm mainly going off of this tutorial

    and the result is supossed to be a ray coming from the camera.
    However, my ray is coming from the transform of the object the script is attached to.
    Code (CSharp):
    1. Vector3 mousePos = Input.mousePosition;
    2.             mousePos.z = 100f;
    3.             mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    4.             //visibility
    5.             Debug.DrawRay(transform.position, mousePos-transform.position, Color.green);
    6.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    7.             RaycastHit hit;
    8.  
    9.             if (Physics.Raycast(ray, out hit, 100f))
    10.             {
    11.                 if (hit.collider.tag == "BrokenChain(Clone")
    12.                 {
    13.                     Debug.Log("Clicked");
    14.                     StartCoroutine(Minigame());
    15.                     return;
    16.                 }
    17.             }
    The first parameter passed into Physics.Raycast should be the point which the ray is cast out from, or am I wrong? https://docs.unity3d.com/ScriptReference/Physics.Raycast.html (Unity Documentation). Because the origin of my ray is not from the camera nor does it ever touch the camera
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,558
    Use named arguments to get this one correct. It's a horrible API.

    Also, don't draw whatever weird noise that is on line 5... draw the
    ray
    that you're actually using!!!

    Always use named arguments with
    Physics.Raycast()
    because it contains many poorly-designed overloads:

    https://forum.unity.com/threads/lay...ke-described-in-the-docs.744302/#post-6355392
     
    clurpslurp likes this.