All objects are selected

Hello everyone,

I attached the script below to three different objects and when I clicked one of these objects all scenes are loaded (in this case all three messages appear).

What’s wrong with my code?

I insert the tag in all three objects!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;

// This script shoud be attached to each object

public class SelectMoveObject : MonoBehaviour
{
    private GameObject button;

    public UnityEvent OnClick = new UnityEvent();


    // Start is called before the first frame update
    void Start()
    {
        button = this.gameObject;  
    }

    // Update is called once per frame
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit Hit;

        if (Input.GetMouseButtonDown(0))
        {
            if (Physics.Raycast(ray, out Hit, 100.0f) && Hit.collider.gameObject && button.CompareTag("GPobject"))
            {
                    //SceneManager.LoadScene("GroundScene");
                    Debug.Log("GroundScene");
                }
                else
                {
                    Debug.Log("AirScene");
                    //SceneManager.LoadScene(airScene);
                }
            }
        }
    }

Since your raycast goes out from your Mouse Position, setup 3x the MonoBehaviour, you will get 3x the same behaviour.

You need to read your code and understand what you try to achieve :smile:

I’m at the beginning, sorry. Btw I didn’t understand what you said, and I need to increase that number every time, so is there any solution? Can you show me?

Steps to success:

  • express your problem in standard terms, such as “Click a button to go to a scene”

  • go to Youtube and work through as many of the 53 million different answers that come back as it takes until you understand what is involved.

  • hint: it is more than just what is in the script, so it is unsuited to someone here trying to describe what to do.

Yes, I will gladly explain at once.

I’m trying to make every object that is instantiated from the prefab folder in the scene clickable.

When I click on the object that will also act as a button, if the object has a specific type of tag, only that object will be moved to another scene along with the player view.

I hope that this is clear.

By default, loading a new scene in Unity destroys everything in the previous scene that was not marked DontDestroyOnLoad (DDOL)

Your choices are to either:

  • mark specific things as DDOL to keep them around,

OR:

  • you can re-create the specific thing you want in the new scene.

Persistent data across scenes is generally handled by a GameManager or other persistent type of datastore. There are tons of youtube tutorials for how to make GameManagers and other long-lived managers.

Yes, I know that. At the moment I need help loading different scenes based on the object tag.

That’s easy!

            if(Hit.collider.CompareTag( "foo_tag"))
            {
                UnityEngine.SceneManagement.SceneManager.LoadScene( "foo_scene");
            }
            if(Hit.collider.CompareTag( "bar_tag"))
            {
                UnityEngine.SceneManagement.SceneManager.LoadScene( "bar_scene");
            }

Thanks. But as you can read above I already did it and it works if I put the script on only one object. If I put the script in more than one it loads only the tag first scene.

Sounds like it’s time to start debugging! Try putting the second object 5000 units away. Does it still hit? Etc.

Start printing object names. What are you hitting? What tag does it have?

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

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

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.

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 or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

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.

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://discussions.unity.com/t/839300/3

Also, shorten up that horrible line 30… nobody should have to work with that much code all in one line.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

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

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

Thank you for these details. By the way, I attach a screenshot of my console when I click on one of the three objects where all of them contain the script I have shown before.

https://i.postimg.cc/2jdMsWJP/Schermata-2022-06-07-alle-19-09-44.png