Click Objects In Unity - How To Click Objects in Unity 5 | Unity Mouse Tutorial

  Рет қаралды 142,768

Bilal

Bilal

Күн бұрын

Пікірлер: 97
@gianmarcopane2806
@gianmarcopane2806 5 жыл бұрын
If someone has trouble in detecting objects, check if the object has a collider on it :)
@theloafdude8304
@theloafdude8304 5 жыл бұрын
Thanks dude. Saved my life there
@gianmarcopane2806
@gianmarcopane2806 5 жыл бұрын
Pigster glad to help :)
@CHRISTAMOSPAGHETTIO
@CHRISTAMOSPAGHETTIO 4 жыл бұрын
Hi there, my objects have colliders but I still have trouble detecting objects: NullReferenceException: Object reference not set to an instance of an object PlayerInteract.Update () (at Assets/PlayerInteract.cs:12)
@gianmarcopane2806
@gianmarcopane2806 4 жыл бұрын
CHRISTAMO SPAGHETTIO there’s no playerInteract script in this tutorial, usually you got this error when you trying to do “something” with an object which is actually null. If you need further help add me on Facebook, I can help you there :)
@廖奕新-k5i
@廖奕新-k5i 4 жыл бұрын
2020.11.06 If someone has trouble in detecting objects that "have" collider, check if your camera's tag is "MainCamera" T_T
@anothermonkeyplaying4773
@anothermonkeyplaying4773 3 жыл бұрын
If someone is trying to do it with sprites, try this: if (Input.GetMouseButtonUp(0)) { RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit.collider != null) { Debug.Log("Target Name: " + hit.collider.gameObject.name); } }
@stiggyman3176
@stiggyman3176 2 жыл бұрын
Thanks a ton :)
@manamage6759
@manamage6759 2 жыл бұрын
Godsend thank you so much :)
@Shiroze
@Shiroze 2 жыл бұрын
My hero
@_Lorenz05
@_Lorenz05 2 жыл бұрын
please someone halp me, why does it not work on UI text? it has a box collider 2D and a square works with this but not UI text. I'm desperate for answers
@Abiodapro
@Abiodapro Жыл бұрын
You saved me! Thanks a lot!
@ryandonaldson3370
@ryandonaldson3370 3 жыл бұрын
I get NullReferenceException: Object Reference not set to an instance of the object. Every time I click this pops up in the console.
@lordsubl1me
@lordsubl1me 2 жыл бұрын
good tuto, actually explained everything
@christopherarendt3531
@christopherarendt3531 4 жыл бұрын
More clear to me: Rigidbody rb = hit.transform.GetComponent(); if(rb) ... You have = in if, which I know sets it and tests the result at the same time, but it is easily overlooked, since usually you use == in conditions.
@buddyroach
@buddyroach 3 жыл бұрын
7:18 how you do a condition without using double equal sign?
@mariatikhomirova7990
@mariatikhomirova7990 3 жыл бұрын
great video, exactly what I was looking for
@Hugo.Digital
@Hugo.Digital 3 жыл бұрын
Amazing help to my projects. Thx.
@flaviopitica
@flaviopitica 6 жыл бұрын
great explanation, thanks! keep up the amazing job
@viktorhyper
@viktorhyper 4 жыл бұрын
Nice tutorial dude! Thx a lot!
@andrescarretero3542
@andrescarretero3542 5 жыл бұрын
If it doesn't work to anyone try this adding this: Camera cam; void Start(){ cam = GetComponent(); } and then in the method Update, replace 'Camera.main' with cam
@meatburger6910
@meatburger6910 4 жыл бұрын
Andres Carretero Thanks so much.
@morpheusbloodeaglegamesoff6506
@morpheusbloodeaglegamesoff6506 3 жыл бұрын
Oh fuck, tnx bruh, u really helped me
@daniyalniazi1771
@daniyalniazi1771 3 жыл бұрын
FIANLLY A GOOD TUTORIAL ON THIS
@DonnyPhan-v7h
@DonnyPhan-v7h Жыл бұрын
Hi, I was wondering how you were able to detect the Ground Click? Did you include a collider?
@jthrash841
@jthrash841 7 жыл бұрын
Thanks. Just what I needed.
@SamCo99
@SamCo99 3 жыл бұрын
Awesome vid, thanks so much! Any words of advice on if I want text to display in game when the player clicks on something? Pretty new to Unity and kinda struggling a bit, but basically I want a text box to appear when the player clicks on something (the camera is first person too). Either way, thanks a bunch!
@kinshuksinghbist7571
@kinshuksinghbist7571 7 жыл бұрын
Hey!Cool video!!! i don't have money but i wanna support you...Well I saw the whole add and clicked on it!!Keep Up!
@itanitv
@itanitv 7 жыл бұрын
+kinshuk bist No worries! Thanks for your support!
@simonsobek7253
@simonsobek7253 2 жыл бұрын
very nice tutorial
@itanitv
@itanitv 2 жыл бұрын
Thank you! Cheers!
@andrew2340
@andrew2340 3 жыл бұрын
Hi. But you're not telling how to prepare game object. what components should be added to the gameobject.
@waffwrw
@waffwrw 4 жыл бұрын
Thank you sooooooo much bro! ily
@Emre-db1kj
@Emre-db1kj 3 жыл бұрын
Thanks for the video! How do you get the recommendations of methods if you type in for example "Input."? Do I have to install some kind of an extension?
@wahyuzulyasyaputra6786
@wahyuzulyasyaputra6786 3 жыл бұрын
Thanks. Helpfully..
@rayjunior3204
@rayjunior3204 5 жыл бұрын
how would you make the ball move to another location in a certain area ??? (so when mouse is clicked the ball should move in a random place between x(400) and y(400))
@pedrogama2707
@pedrogama2707 4 жыл бұрын
AWESOME, thanks!
@coachwayneevans2480
@coachwayneevans2480 3 жыл бұрын
brilliant. thank you
@muhammadsubtainmustafa5107
@muhammadsubtainmustafa5107 7 жыл бұрын
You are Amazing >> Best Video i see
@itanitv
@itanitv 7 жыл бұрын
Thanks!
@linushallgren4156
@linushallgren4156 3 жыл бұрын
I had pretty much the same code yet id did not work before watching the video and the issue was how far the rays went :(. This video still helped me solve it but so frustrating that something so simple had made me waste half an hour
@Jacktherippler1
@Jacktherippler1 6 жыл бұрын
hey mate, i created my own hextile in blender. now i added a box collider to the hex and its working. but i got 10000 tiles and dont want to add it to every single one. i cant make copies of the one hex with the boxcollider. any other ideas? thank you so much! awesome example!good job
@ImprovedTruth
@ImprovedTruth 6 жыл бұрын
In case you didn't figure this out, after you create an object in code: hextile.AddComponent();
@Jacktherippler1
@Jacktherippler1 6 жыл бұрын
@@ImprovedTruth thanks mate! I iterate over every tile and did every joob you said!
@Jacktherippler1
@Jacktherippler1 6 жыл бұрын
@@ImprovedTruth thanks a lot!!!
@woshstudios
@woshstudios 4 жыл бұрын
This was super helpful, how do you combine this selection with say a click to move along a navmesh?
@louisdaisomont490
@louisdaisomont490 3 жыл бұрын
I had an error because i changed my camera, so the Camera.main.ScreenPointToRay(Input.mousePosition) wasn't working because my camera hadn't the tag MainCamera Take care to that if you are in the same case as me
@ceb1ueseamcbride220
@ceb1ueseamcbride220 2 жыл бұрын
When I tried to use the code at 5:40 it says, "cannot convert from 'string' to 'UnityEngine.GameObject'" I am not sure what's wrong here's my code I am also using 2d 2021.3.5f1 using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectClickerScript : MonoBehaviour { private void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100.0f)) { if (hit.transform != null) { PrintName(hit.transform.gameObject); } } } } private void PrintName(GameObject go) { PrintName(go.name); } }
@fabianhofmann9356
@fabianhofmann9356 4 жыл бұрын
My brain has a melt down.
@JustJory
@JustJory 4 жыл бұрын
its freezing for me because i am pausing trying to think all the time
@sagargada73
@sagargada73 3 жыл бұрын
This script works with normal object, but when i import my model eg. a building, it does not detect my model, if anyone knows how to achieve that drop a comment! help me out!
@blaiseflorendo3967
@blaiseflorendo3967 6 жыл бұрын
does it work with mobile inputs too?
@itanitv
@itanitv 6 жыл бұрын
Blaise Florendo Yup, but you would have to use the Input.touches array for a screen point rather than the mouse position, and check if the touches arra's length is greater than zero
@blaiseflorendo3967
@blaiseflorendo3967 6 жыл бұрын
Xenfinity Okay. Thanks!
@jswerve6823
@jswerve6823 5 жыл бұрын
My raycast hits my canvas before it can hit an object. how do I ignore this case?
@itanitv
@itanitv 5 жыл бұрын
I'll recommend using a layermask for everything except whatever you don't want to hit. So for example your Canvas probably has the layer "UI." So you'll want to get a reference to the layermask of that layer like so: var mask = LayerMask.NameToLayer("UI") Then you can use the Physics.Raycast definition to pass a layermask of everything that is NOT the UI mask using the ~ operator. For example: Physics.Raycast(position, direction, out hit, Mathf.Infinity, ~mask)) Let me know if you need any more help!
@StaryzHelios
@StaryzHelios 5 жыл бұрын
how about 2d?
@itanitv
@itanitv 5 жыл бұрын
You would likely want to use Physics2d.raycast instead!
@CirbyWeh
@CirbyWeh 5 жыл бұрын
@@itanitv I tried this in my 2D project and it works fine, with a big but: When I have two overlapping colliders, it only detects the object closer to the camera (Z position). Changing the Sorting Layer or Order in Layer does only change the visual appearance. So worst case scenario: ObjectA is visually completely hidden behind ObjectB, but I can only click on ObjectA. Do you have an idea how to fix this, without changing the Z positions?
@general_md
@general_md 4 жыл бұрын
Use "void onMouseDown()", it detects the click on an object with a collider on it as "is trigger"
@wuimingleebarroga8629
@wuimingleebarroga8629 5 жыл бұрын
if i have 2 object with different movement when clicked, how can i do that?
@itanitv
@itanitv 5 жыл бұрын
You could create another script with different behavior! Or you could have a a serialized or public field and change it in the inspector for each game object
@wuimingleebarroga8629
@wuimingleebarroga8629 5 жыл бұрын
@@itanitv thanks
@itanitv
@itanitv 5 жыл бұрын
No problem! Let me know if you still have any questions
@wuimingleebarroga8629
@wuimingleebarroga8629 5 жыл бұрын
@@itanitv sure thanks btw
@GEORGE.M.M
@GEORGE.M.M 7 жыл бұрын
Hey Great tutorial!! quick question, how would I select and object using the raycast I'm operating using the xbox controller, instead of a mouse? RaycastHit hit; float theDistance; //Debug Raycast in the Editor - SO WE CAN SEE IT! - MAKES LIFE EASIER! //Vector3 forward = transform.TransformDirection(Vector3.forward) * 10; Vector3 back = new Vector3(Input.GetAxis("XBOX.1.RightStick.X") * Time.deltaTime * 10, Input.GetAxis("XBOX.1.LT") * Time.deltaTime * 10, Input.GetAxis("XBOX.1.RightStick.Y") * Time.deltaTime * 10); Debug.DrawRay(transform.position, back, Color.green); if (Physics.Raycast(transform.position, (back), out hit)) { if (hit.collider.gameObject.tag == "tile") { theDistance = hit.distance; print(theDistance + " " + hit.collider.gameObject.name); if (Input.GetButton("XBOX.1.RightStick.Press")) { Cube.Selected = this.gameObject; } } } I created a separate class called raycastFoward, and the item I want to select is a class called Cube. I end up selecting the drawn ray, Im trying to select a game of object from another class instead, is this the wrong way to pass the argument, plus my script is attached to a game object child of my main camera.
@GenoFelice
@GenoFelice 7 жыл бұрын
Why not use OnMouseEnter instead of a ray. What is the advantage?
@itanitv
@itanitv 7 жыл бұрын
More control. You could use the function "OnMouseClick," but it makes a lot of assumptions and forces you to do everything only in that function's context. For example, you may want to only click within a certain distance before the clicked object is retrieved (raycast with distance vs click, then distance check)
@GenoFelice
@GenoFelice 7 жыл бұрын
Makes sense
@sode07
@sode07 3 жыл бұрын
ScreenPointToRay is not clear to a beginer it basically makes a screen point in to a ray xD I laughed so hard
@Aresvfxx
@Aresvfxx 4 жыл бұрын
Can i use this in a 2d game?
@cristiancarrasco9371
@cristiancarrasco9371 4 жыл бұрын
i am trying but it is not working for me
@Aresvfxx
@Aresvfxx 4 жыл бұрын
@@cristiancarrasco9371 i have a working script i'll send it to you when i get back home
@k-kayla
@k-kayla 3 жыл бұрын
Did you manage to figure it out?? I'm wondering the same thing.
@williamkyburz
@williamkyburz 6 жыл бұрын
Thank you
@yigitcan9424
@yigitcan9424 5 жыл бұрын
Thanks.
@jasonl9266
@jasonl9266 5 жыл бұрын
What about touch ?
@yigithanion
@yigithanion 5 жыл бұрын
Thanks
@bilbostabbins1882
@bilbostabbins1882 4 жыл бұрын
why was your audio so scuffed lol
@giorgiamashukeli941
@giorgiamashukeli941 5 жыл бұрын
its says it is not insantce object
@itanitv
@itanitv 5 жыл бұрын
Which line are you getting that on? Does your target have a collider and rigidbody?
@CHRISTAMOSPAGHETTIO
@CHRISTAMOSPAGHETTIO 4 жыл бұрын
@@itanitv Hi, I have the same issue, I have tried turning collider/rigidbody on and off but I still get this same issue: NullReferenceException: Object reference not set to an instance of an object PlayerInteract.Update () (at Assets/PlayerInteract.cs:12)
@CHRISTAMOSPAGHETTIO
@CHRISTAMOSPAGHETTIO 4 жыл бұрын
Somehow, changing the 'player' tag from my main camera to 'main camera' has it working, thanks for the tutorial! XD
@itanitv
@itanitv 4 жыл бұрын
Yeah that's why Camera.main is not a great field to reference. It's actually a property rather than an attribute so it's not a constant time access. It actually searches the entire hierarchy of game objects in you scene to find a camera tagged "MainCamera." It's a poor system and gets slower as you add more game objects to a scene, but it works for tutorials to avoid going off topic
@CHRISTAMOSPAGHETTIO
@CHRISTAMOSPAGHETTIO 4 жыл бұрын
@@itanitv I'm not entirely sure what you just said but I am making my first game and it feels like you are saying I should change something to do with my main camera? Could you possibly dumb it down for me, I only started using Unity last night XD
@benckis
@benckis 6 жыл бұрын
PrintName doent exist in curent context
@Jacktherippler1
@Jacktherippler1 6 жыл бұрын
you have to create a method with this name or just use print("xyz");
@D-K-C
@D-K-C 4 жыл бұрын
Ъ
@DoomCatcher
@DoomCatcher 3 жыл бұрын
I would personally prefer you to do tutorials focused on the topic at hand and going into more detail rather than adding flashy stuff such as making the ball bounce.
ScriptableObjects, Explained | Unity Tutorial
19:27
LlamAcademy
Рет қаралды 13 М.
The Ultimate Introduction to Scriptable Objects in Unity
20:45
JISOO - ‘꽃(FLOWER)’ M/V
3:05
BLACKPINK
Рет қаралды 137 МЛН
Война Семей - ВСЕ СЕРИИ, 1 сезон (серии 1-20)
7:40:31
Семейные Сериалы
Рет қаралды 1,6 МЛН
Every team from the Bracket Buster! Who ya got? 😏
0:53
FailArmy Shorts
Рет қаралды 13 МЛН
How to convert the mouse position to world space in Unity
18:14
Game Dev Beginner
Рет қаралды 38 М.
How to make a Video Game - Godot Beginner Tutorial
1:17:12
Brackeys
Рет қаралды 2,7 МЛН
My Favorite way to Click on GameObjects
4:51
VR with Andrew
Рет қаралды 52 М.
The Power of Scriptable Objects as Middle-Men
17:41
samyam
Рет қаралды 133 М.
How To Use Scriptable Objects in Unity
4:28
Game Dev Guide
Рет қаралды 109 М.
Unity Tutorial: Open Panel on Button Click
3:39
Jayanam
Рет қаралды 176 М.
HOW TO SWITCH CAMERAS ON KEY PRESS WITH C# UNITY TUTORIAL
7:20
Jimmy Vegas
Рет қаралды 58 М.
How do non-euclidean games work? | Bitwise
14:19
DigiDigger
Рет қаралды 2,5 МЛН
SCRIPTABLE OBJECTS in Unity
8:57
Brackeys
Рет қаралды 1 МЛН
Detect Clicks on Gameobject in Unity | Tutorial
5:14
Luwill
Рет қаралды 3,7 М.
JISOO - ‘꽃(FLOWER)’ M/V
3:05
BLACKPINK
Рет қаралды 137 МЛН