If someone has trouble in detecting objects, check if the object has a collider on it :)
@theloafdude83045 жыл бұрын
Thanks dude. Saved my life there
@gianmarcopane28065 жыл бұрын
Pigster glad to help :)
@CHRISTAMOSPAGHETTIO4 жыл бұрын
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)
@gianmarcopane28064 жыл бұрын
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 :)
@廖奕新-k5i4 жыл бұрын
2020.11.06 If someone has trouble in detecting objects that "have" collider, check if your camera's tag is "MainCamera" T_T
@anothermonkeyplaying47733 жыл бұрын
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); } }
@stiggyman31762 жыл бұрын
Thanks a ton :)
@manamage67592 жыл бұрын
Godsend thank you so much :)
@Shiroze2 жыл бұрын
My hero
@_Lorenz052 жыл бұрын
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 Жыл бұрын
You saved me! Thanks a lot!
@ryandonaldson33703 жыл бұрын
I get NullReferenceException: Object Reference not set to an instance of the object. Every time I click this pops up in the console.
@lordsubl1me2 жыл бұрын
good tuto, actually explained everything
@christopherarendt35314 жыл бұрын
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.
@buddyroach3 жыл бұрын
7:18 how you do a condition without using double equal sign?
@mariatikhomirova79903 жыл бұрын
great video, exactly what I was looking for
@Hugo.Digital3 жыл бұрын
Amazing help to my projects. Thx.
@flaviopitica6 жыл бұрын
great explanation, thanks! keep up the amazing job
@viktorhyper4 жыл бұрын
Nice tutorial dude! Thx a lot!
@andrescarretero35425 жыл бұрын
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
@meatburger69104 жыл бұрын
Andres Carretero Thanks so much.
@morpheusbloodeaglegamesoff65063 жыл бұрын
Oh fuck, tnx bruh, u really helped me
@daniyalniazi17713 жыл бұрын
FIANLLY A GOOD TUTORIAL ON THIS
@DonnyPhan-v7h Жыл бұрын
Hi, I was wondering how you were able to detect the Ground Click? Did you include a collider?
@jthrash8417 жыл бұрын
Thanks. Just what I needed.
@SamCo993 жыл бұрын
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!
@kinshuksinghbist75717 жыл бұрын
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!
@itanitv7 жыл бұрын
+kinshuk bist No worries! Thanks for your support!
@simonsobek72532 жыл бұрын
very nice tutorial
@itanitv2 жыл бұрын
Thank you! Cheers!
@andrew23403 жыл бұрын
Hi. But you're not telling how to prepare game object. what components should be added to the gameobject.
@waffwrw4 жыл бұрын
Thank you sooooooo much bro! ily
@Emre-db1kj3 жыл бұрын
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?
@wahyuzulyasyaputra67863 жыл бұрын
Thanks. Helpfully..
@rayjunior32045 жыл бұрын
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))
@pedrogama27074 жыл бұрын
AWESOME, thanks!
@coachwayneevans24803 жыл бұрын
brilliant. thank you
@muhammadsubtainmustafa51077 жыл бұрын
You are Amazing >> Best Video i see
@itanitv7 жыл бұрын
Thanks!
@linushallgren41563 жыл бұрын
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
@Jacktherippler16 жыл бұрын
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
@ImprovedTruth6 жыл бұрын
In case you didn't figure this out, after you create an object in code: hextile.AddComponent();
@Jacktherippler16 жыл бұрын
@@ImprovedTruth thanks mate! I iterate over every tile and did every joob you said!
@Jacktherippler16 жыл бұрын
@@ImprovedTruth thanks a lot!!!
@woshstudios4 жыл бұрын
This was super helpful, how do you combine this selection with say a click to move along a navmesh?
@louisdaisomont4903 жыл бұрын
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
@ceb1ueseamcbride2202 жыл бұрын
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); } }
@fabianhofmann93564 жыл бұрын
My brain has a melt down.
@JustJory4 жыл бұрын
its freezing for me because i am pausing trying to think all the time
@sagargada733 жыл бұрын
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!
@blaiseflorendo39676 жыл бұрын
does it work with mobile inputs too?
@itanitv6 жыл бұрын
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
@blaiseflorendo39676 жыл бұрын
Xenfinity Okay. Thanks!
@jswerve68235 жыл бұрын
My raycast hits my canvas before it can hit an object. how do I ignore this case?
@itanitv5 жыл бұрын
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!
@StaryzHelios5 жыл бұрын
how about 2d?
@itanitv5 жыл бұрын
You would likely want to use Physics2d.raycast instead!
@CirbyWeh5 жыл бұрын
@@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_md4 жыл бұрын
Use "void onMouseDown()", it detects the click on an object with a collider on it as "is trigger"
@wuimingleebarroga86295 жыл бұрын
if i have 2 object with different movement when clicked, how can i do that?
@itanitv5 жыл бұрын
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
@wuimingleebarroga86295 жыл бұрын
@@itanitv thanks
@itanitv5 жыл бұрын
No problem! Let me know if you still have any questions
@wuimingleebarroga86295 жыл бұрын
@@itanitv sure thanks btw
@GEORGE.M.M7 жыл бұрын
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.
@GenoFelice7 жыл бұрын
Why not use OnMouseEnter instead of a ray. What is the advantage?
@itanitv7 жыл бұрын
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)
@GenoFelice7 жыл бұрын
Makes sense
@sode073 жыл бұрын
ScreenPointToRay is not clear to a beginer it basically makes a screen point in to a ray xD I laughed so hard
@Aresvfxx4 жыл бұрын
Can i use this in a 2d game?
@cristiancarrasco93714 жыл бұрын
i am trying but it is not working for me
@Aresvfxx4 жыл бұрын
@@cristiancarrasco9371 i have a working script i'll send it to you when i get back home
@k-kayla3 жыл бұрын
Did you manage to figure it out?? I'm wondering the same thing.
@williamkyburz6 жыл бұрын
Thank you
@yigitcan94245 жыл бұрын
Thanks.
@jasonl92665 жыл бұрын
What about touch ?
@yigithanion5 жыл бұрын
Thanks
@bilbostabbins18824 жыл бұрын
why was your audio so scuffed lol
@giorgiamashukeli9415 жыл бұрын
its says it is not insantce object
@itanitv5 жыл бұрын
Which line are you getting that on? Does your target have a collider and rigidbody?
@CHRISTAMOSPAGHETTIO4 жыл бұрын
@@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)
@CHRISTAMOSPAGHETTIO4 жыл бұрын
Somehow, changing the 'player' tag from my main camera to 'main camera' has it working, thanks for the tutorial! XD
@itanitv4 жыл бұрын
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
@CHRISTAMOSPAGHETTIO4 жыл бұрын
@@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
@benckis6 жыл бұрын
PrintName doent exist in curent context
@Jacktherippler16 жыл бұрын
you have to create a method with this name or just use print("xyz");
@D-K-C4 жыл бұрын
Ъ
@DoomCatcher3 жыл бұрын
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.