3D Survival Game Tutorial | Unity | Part 3: Selecting Items with Raycast & Creating Simple AI
ฝัง
- เผยแพร่เมื่อ 5 ก.ย. 2024
- #survivalgame #tutorial #unity
In this tutorial series, we will create a 3D survival game with Unity & C# as the scripting language.
We will start with basic FPS movement, inventory building, and crafting systems.
Learn how to pick up items and more.
Little by little, we are going to add different features that are common to survival open-world games.
This series is a bit more advanced and fast-paced, but I will try to explain everything I do.
Any questions are welcome.
Selection Manager Script:
gist.github.co...
InteractableObject Script:
gist.github.co...
Ai Movement Script:
gist.github.co...
White Rabbit free asset:
assetstore.uni...
Sphere/Circle Sprite
drive.google.c...
Survival Series Playlist:
• Unity 3D | Open-World ...
💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾
Between my full-time job and my family life,
I try to find free time to create content for this channel.
You can support me and help this channel keep growing:
paypal.me/Mike...
💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾💾
💻 This Is My Development Setup (Affiliate): 💻
============
Main Monitor:
amzn.to/3M64qCJ
Secondary Monitor:
amzn.to/41Iu06A
Graphics Card:
amzn.to/3MpnXzd
CPU:
amzn.to/3I8nvCW
RAM:
amzn.to/42zqM6u
Keyboard:
amzn.to/3W5RFN4
Mouse:
amzn.to/3nTPcZs
Headphones:
amzn.to/3pz0By5
Microphone:
amzn.to/3OecJz3
For anybody getting the object not set to an instance ...
And that is using TextMeshPro in the newer Unity Versions
Here is what you should do
Add this to the top of the selectionmanager:
using TMPro;
Then u also have to change these 2 lines:
Text interaction_text; --------------> TextMeshProUGUI interaction_text;
interaction_text = interaction_Info_UI.GetComponent(); ---------------> interaction_text = interaction_Info_UI.GetComponent();
I hope this helps :)
Thanks bro, you make my day easier :D
@@gibenkozak4448 No Worries, I didn't want anyone else to go through the same struggle haha
You rock, thank you!
bloody legend thanks a lot man I was pulling hair out because of this.
Legend for telling everybody 👍👍
First off, amazing tutorials and explanations! The best I have came across, great at explaining the little details.
I laughed so hard when you were creating the rabbits and you went over one and said "oh I stepped on one" as if the rabbit felt it.
Keep doing what you are doing, we really appreciate it!!!
If your like me and don't want the selectionmanager to select stuff across the map, you can simple change this
if (Physics.Raycast(ray, out hit))
to
if (Physics.Raycast(ray, out hit, 10))
Great advice, thanks. But when you do so, the text update only occurs within that range and for instance if you hover over that object and move away while looking towards it, it (text object) will be stuck on that state and only will be updated when nearby an interactable object or the first one you locked onto.
@@berkeaytan5506 This is the solution to that
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactRange))
{
if(hit.collider.GetComponent() != null)
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent())
{
interactionText.text = selectionTransform.GetComponent().GetItemName();
interaction_Info_UI.SetActive(true);
}
}
else
{
interaction_Info_UI.SetActive(false);
}
}
if(hit.collider == null)
interaction_Info_UI.SetActive(false);
I also added a public float, which is what the "interactRange" is
@@connorpayne2671 Nice job, keep following this tutorial! I took a long break but once you start learning more, unity becomes very fun and a very powerful engine to use.
Alright, first of all, I'm loving this tutorial series. I'm planning to follow them through until I have something cool playable.
Second, reading some comments about problems other had, and making a little research, I found a way to use TextMeshPro instead of the legacy text.
your code should be something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class SelectionManager : MonoBehaviour
{
public GameObject InteractionInfo;
TextMeshProUGUI interaction_text;
private void Start()
{
interaction_text = InteractionInfo.GetComponent();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
InteractableObject interactable = selectionTransform.GetComponent();
if (interactable)
{
InteractionInfo.SetActive(true);
interaction_text.text = interactable.GetItemName();
}
else
{
InteractionInfo.SetActive(false);
}
}
}
}
The gist of it is using TextMeshProUGUI as a variable type, instead of just TextMeshPro. Don't forget to use "using TMPro" at the top of the code too. Hope this helps someone.
helped me a ton although I got a ton of errors and have no clue what to do
nevermind fixed
Thank you so much!!!! @daywedie
Thank you brother!
My code problem 'InteractableObject' could not be found, pls help me 😢
for anyone who has problems with the item name not showing when you look at an object, Make sure that the object has a collision box. I've been pulling my hair out for hours with this problem, yet the solution was kind of obvious haha. Could just be a problem for me because I'm using different assets. anyways, love your videos and how you explain everything. Thanks man
The asset I'm using has colliders already but I am still having that problem... Any suggestions on how to fix it?
Thanks bro, I was also pulling my hair out
The solution to all my problems xD. Thanks
If you cant find the animator tab, its under window in the ribbon alog the top window/animation/animator
if you are getting The type or namespace name 'InteractableObject' could not be found. its because you need to create a seperate c# script named 'InteractableObject' and then place the code in description for it in it.
g
For anybody have a problem with the rabbit face down when idle state, you should freeze the x and y rotation in rigidbody
ồ thank kiu bro.
Hey Mike, Thanks for the tutorial. Just a small suggestion: it's always very helpful to provide the assets you use in your tutorial to the viewer. For example, even something simple as importing that white sphere you use for the reticle took me like two hours since I needed to download image software and figure out how to create the right type of image.
You can adjust the type of image, change to sprite etc inside of Unity. Just make sure you have the 2D sprite addon added in the package manager. Once installed just click on the image in the project window and in the inspector you can change it to whatever you need. Sprite, cursor whatever. Window > package manager > select unity registry and install from there. There are tons of tools in there for image and atlas editing. Cheers
for future reference there is a thing called knob in the 2023 version for me not sure were it came from but it does not go to anything ive imported and its already pre made for crosshair
@@SAVAGEGHXST420 It's for the sliders. I'm pretty sure they've been around for a while. I'm using the 2022 version and I have it too, so that's what I was using
Im really enjoying going over these videos,
Ive never really working in 3d, and these explanations are really good.
Ive had various problems when adding stuff to the game world, and had a lot of fun tracking down the issues, and figuring out where I went wrong.
An addition to your raycast would be a float for ray distance, otherwise you will hit everything in the game world. So = if(Physics.Raycast(ray, out hit, 2f)).
It is a good solution but... When i increased a distance from this object the text won't disappear. Any idea?
@@InfidelAtWork bool isHit = Physics.Raycast(ray, out hit, 2f);
if (isHit)
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent())
{
interaction_text.text = selectionTransform.GetComponent().GetItemName();
interaction_Info_UI.SetActive(true);
}
}
else
{
interaction_Info_UI.SetActive(false);
}
best tutorials i ever seen about unity
thank you so much
21:07 you might already know this but if you hold right mouse button you can use wasd to move
If you are having trouble with the selection script you first also have to have the interactable object scripts
what do you put the interactable object script on?
Any object like a rabbit or rock that you want to interact with
Mike thank you for your tutorials!
It's motivate me to explore all details and even try to do some modifications.
I subscribed because of how good this tutorial is :)
One of the best tutorials out there !
Hey guys, I'm sorry for those who are looking for answers to their errors, so if you're getting the error about the interactable object in the selection manager script all you need to the is make sure you have the interactable script imported with the stuff mike has. hope this helped some of u
Thank you broo 👍🏿👍🏿
thank you so so so much
READ IF YOU CANNOT GET YOUR TEXT TO SHOW WHEN YOU HOVER OVER AN INTERACTABLE OBJECT: (this worked for me)
-Ensure that the interactable object has a collider on it. If its an asset that you made yourself or different to the one Mike is using, input a mesh collider
- when you click play, make sure you actually click into the game (ur PC Cursor should disappear when you click into the game). Your camera will be able to move while ur not actually clicked into the game, but the UI won't work untill your cursor disappears by clicking into the game.
This should fix the problem.
I have a problem with the script, mine says:
Assets\Scripts\SelectionManager.cs(25,49): error CS0246: The type or namespace name 'InteractableObject' could not be found (are you missing a using directive or an assembly reference?)
how do i fix this?
@@FlamberetElg at the top of the script, you should have "using UnityEngine;"
Underneath it, type using "UnityEngine.UI;"
This might fix the problem. give it a shot
@@FlamberetElg Is this what I did, and looked at the code, so you needed to keep on the video for a bit until you create the interactable script?
also mine didnt work, until I figured out I was using TextMesh Pro, that calls the text component in a different way, as soon as I moved it back to Legacy, it was fine.
Error whiile in playmode: "object reference not set" on SelectionManager script copy and paste this,
Note: In the unity editor you will need to drag the players camera into the "Camera" reference
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class SelectionManager : MonoBehaviour
{
public Camera playerCamera;
public GameObject InteractionInfo;
TextMeshProUGUI interaction_text;
private void Start()
{
interaction_text = InteractionInfo.GetComponent();
}
void Update()
{
Ray ray = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
InteractableObject interactable = selectionTransform.GetComponent();
if (interactable)
{
InteractionInfo.SetActive(true);
interaction_text.text = interactable.GetItemName();
}
else
{
InteractionInfo.SetActive(false);
}
}
}
}
Hope this helps
Brooo! Thank you so much I've been struggling finding the solutions and I see your comment and it solved the problem! Thank youuu have a nice day!
Hey man, this seems to be helping for me but idk how to drag the players camera into the "Camera" reference lol, do you think you could describe it a little bit more for me?
Thank you so much!
Thank you so muchhhhhhhh I was about to quit my game development bcuz of this
@@linctheweightlifter04 just drag the main camera object under your player parent to the selection manager inspector
Hi, I like the way you explain, glad I found you. I will definitely follow this series. It would also be good if, during the explanation, you would add how the script would be done if, for example, instead of the first person, you used the third person, what would the difference be.. As for the rest, it's a really good tutorial... Best regards, and keep up the good work ...subscribed
It would be pretty much the same. You just would want to have a humanoid for a player and move the position of the camera so you can see the player. From what I know that is all you would really have to do
So here's an interesting question.
Here's the setup: The trees that I'm using to do this tutorial are from a pack I've already purchased. I did everything correctly to set up the text per Mike's instructions. But it just wasnt doing anything. So after double checking it, I added some debug messages to the SelectionManager Script.. I had it tell me after the hit what transform it was finding, and after the if statement where it was looking for InteractableObject, whether or not if found it.
It was making a hit, but the transform it returned was the LOD0, not the parent. So, it was not finding the Interactable Object. It took me a second to recognize that it wasnt testing the parent of the Tree... because the hit was being made on it's LOD0... and so when I put InteractableObject on it's LOD it worked fine... It didnt matter whether or not the tree was rendering LOD4, as long as the code was attached to LOD0 it worked fine... Does this sound like it's working as designed to you all? Do you think I should report it to Unity as a bug? Anyway for anyone using a different tree... Hopefully this comment will help, and hopefully it will save you the proverbial banging of the head against the wall i did.
After looking deeper into this it's because the asset creator added the capsule collider to LOD 0, not to the parent. So, after putting the collider on the parent it all worked just fine... be aware if you're having this problem, that leaving the interaction on the LOD will only let you interact with the LOD... So, thats not really a solution. I.e. if you were to have the cut tree down script on that LOD you'd only be cutting down the LOD. It would look normal when you are up close and actually interacting with the tree, BUuuuuut when you move away from the tree LOD 1 would materialize as if it were magic... and then when you move closer to the tree, and it tries to load LOD 0... the tree would then disappear again... so if you are having the problem that I first described... move the Collider to the main parent of the tree.
Also: Change the Box Collider to enable Trigger. Next Add Navmesh agent component to each the rabbit prefab and set Base Offset to like -0.10. Disable the original AI_Movement Script or replace it with AI_Movement_Navmesh script. Add a NavMeshSurface (if you don't have one already) component to your Terrain and Bake. (I use Unity 2022.3.25f1)
If you are getting :
NullReferenceException: Object reference not set to an instance of an object
SelectionManager.Update () (at Assets/Scripts/SelectionManager.cs:21)
make sure your camera is tag MainCamera
i am
If text doesn't appear when you hover over the object with cursor, try adding collider to your prefab. That's how it worked in my project.
im kinda dumb, but whats a prefab
nvm
@@Warp_Speed_Studios Actually, I don't know exactly 😄Probably some sort of object in the game.
I have this really weird issue, everything is working fine but when the rabbits are idle on a sloped surface they just face plant into the ground and try to hop into the ground, when the idle timer is up and they start start to run again it goes back to normal until they idle on a slope again. does anyone have the same problem or does anyone know how to fix it
Hey, had the same problem, it is actually fixed in the next episode, in case you didn't see it, jus go into your rabbit's prefab and in the rigidbody>constraints, freeze the x and z rotation. It should fix everything
For the most part it is working, the only problem is that the interactable text doesn't disappear when I look away from the object everytime.
Not sure if this is a collision issue, I am using the mesh collider for my objects. It is almost as if it randomly decides when the text should disappear. I am using legacy text not text mesh pro.
He talks about it in the 4th part if anyone found this.
i have the same problem@@skoonahyvampire6084
Awesome tutorials! TYSM! 💪
i love you,this is best tutorials i've seen
how come when im trying to sort out the selection manager the interaction info ui doesnt show up
please help
U first must have the interactable script it doesnt matter for where u are at if its on anything.
I have issue on this selectionmanager script it gives me null reference object error please help mei to fix this
yes please
for the sphere, i just used the UI text and plus sign
Hi Mike I have 1 problem after I write "using UnityEngine.UI" on the top of script I have this error Assets\Scripts\SelectionManager.cs(1,21): error CS1002: ; expected Can you help me?
You are mising " ; "
If you’re having trouble with the text not showing and its not to do with colliders or ur mouse, I realised that I had 2 cameras operating, one main camera and one camera for the player. Because the script is directed at the main camera it wasnt detecting any rays! I changed the code to be camera.current instead of camera.main (but i think a better solution would be to have only ur main camera active I am just too tired to be bothered to change everything rn). Hope this helps!!!
When i reach the point of adding the component Selection Manager i dint get the Interaction_info_UI, the same when i add the component Interactable object i dont get the Item Name option that appears in your video. it seems that many are having this issue. is there a way to fix this, i tried everything so far, out of ideas...
Hi, have you found a solution to this problem?
I got this error what do i do: Assets\Scripts\SelectionManager.cs(9,43): error CS1002: ; expected
Its for the selection manager
the scriptmanager file doesnt come up with the 2 boxs when i drop the file on the enmty object yet i name eveything as i should so what could i be doing wrong? i am using the latest unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SelectionManager : MonoBehaviour
...
But nothing, in inspector the "Selection Manager" script does not add the "Interaction_Info_UI" option
Got the exact same problem
@@oArzoh at minute 14 he explains it (he could have said it earlier...):
a hero without a cape
error i cant seem to fix
Assets\SelectionManager.cs(11,5): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)
Here is updated script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SelectionManager : MonoBehaviour
{
private TextMeshProUGUI textMeshPro;
public GameObject interaction_Info_UI;
TextMeshProUGUI interaction_text;
private void Start()
{
textMeshPro = GetComponent();
interaction_text = interaction_Info_UI.GetComponent();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent())
{
interaction_text.text = selectionTransform.GetComponent().GetItemName();
interaction_Info_UI.SetActive(true);
}
else
{
interaction_Info_UI.SetActive(false);
}
}
}
}
write "using Unity.Engine.UI" on the top of your code
When my bunnies stop hopping and they should sit still (idle) they kinds slide forward slowly.. then they turn and hop around, then stop and slide slowly.... lol. Any ideas? The rotation on x and z is locked so that bit is solved. They sit still and slide like they're on a skating ring.
I have elevated terrain, and every single time they go inside the terrain.... how can I fix this?
The ground level for my terrain is y position 0, My rabbits y position is 0 as well. For some reason when I start the game, the rabbit's y position will jump up to .25 and stay there while the game runs indefinitely. This problem causes the rabbit to look like it is slightly floating while jumping around. If I use the constraint on the rigidbody to freeze the y position...It messes with the speed of the rabbit and the animation itself. Anyone else experiencing this problem? can't think of any other fixes for it.
sounds like the collider is lower then the rabit, making the rabit seems like floating because the colider touches the ground (the colider is the actual rabit) and the mesh (the rabit) is above so check that thing for furture 'floating' missunderstanings (the collider needs to be at the very feet of the mesh/rabit, maybe even .01 upper then the mesh but never lower)
@@asylumblack9122 I finally figured it out, and that is exactly what the problem was. I was making different sized rabbits and the box collider wasn't scaling with the resizing of the rabbits and I didn't catch it. I didn't have the rabbit selected in the Hierarchy tab to see what the box collider looked like on it in the scene tab when running the game. If I hadn't of figured it out on my own...I was planning on coming back here this week to see if anyone replied. I appreciate the help! Thank you sir.
at 08:27 when i drag the script to the empty object, i don't get the second row called "Interaction_info_UI". Any solutions to this? I also get this error message when pasting the SelectionManager script code into visual studio: Assets\Scripts\SelectionManager.cs(9,5): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)
I tried doing this with both the TextMeshPro and the normal Text but still the same error
Add using UnityEngine.UI; on the top
@@castleexe2968 Ty for answer man but that didn't work either :/ still no window for interaction_info_ui
@@oArzoh i also dont have the window
14:00 to about 17:55 he explains it
Here is updated script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SelectionManager : MonoBehaviour
{
private TextMeshProUGUI textMeshPro;
public GameObject interaction_Info_UI;
TextMeshProUGUI interaction_text;
private void Start()
{
textMeshPro = GetComponent();
interaction_text = interaction_Info_UI.GetComponent();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent())
{
interaction_text.text = selectionTransform.GetComponent().GetItemName();
interaction_Info_UI.SetActive(true);
}
else
{
interaction_Info_UI.SetActive(false);
}
}
}
}
{can someone help please} ive ran through this vid before on other projects and the [Selectionmanager] and [interactableobjects] files (WORKED) but now they dont show names and ive ran through it over and over and cant fix it
If anyone else is getting “object not set to instance” what worked for me was going to my Camera and changing the tag to Main camera
Did all like in the tut and got this
Assets\Scripts\SelectionManager.cs(10,5): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)
for all who strugle with this i figured it out, he does not mention that you have to add " using UnityEngine.UI;" in the manager script on top.
i can also recomend do not to follow the tutorial step by step because it will lead to errors look the full video then try it. because he mention script that are needet before to late, this will lead to errors if you try to recreate step by step.
@@psychologe112 ty
Make sure you have the 5th script created like in the video :
"using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteractableObject : MonoBehaviour
{
public string ItemName;
public string GetItemName()
{
return ItemName;
}
}"
@@psychologe112 thanks
@@pureluck8882 THANX BRO
im new, and in the newer unity following this tutorial and all is working except the animator section. I cannot for the life of me figure out how to add the isrunning and isdead variables anywhere as mine opens up as read only. any ideas? the bunny stays in the idle animation and moves around per the code, randomly choosing a direction and time, it just doesnt swap from the idle to the running animation. ***edit but leaving for others*** under window/animation/animator parameters is the window to add the isrunning and isdead, it is not in the animation window, its in the animator window, which is activated in the same place. window/animation/animator.
I love you thank you!😂
Help, the item name appears when my cursor is on the object and not when the middle white dot is on the object, any ways to fix this?
@@myhammyxiaojiji The cursor should be locked to the middle of the screen + invisible.
fantastic video u helped me a lot
Please can you link your scripts; I like this form of tutorial, you made it verry well
Hello! Very good video, it helped me a lot, but I have a problem, and that is that when the rabbit stops walking and has to wait while still, instead of staying still, it continues walking but down ( -Y ) and then continues walking in one direction.
Really great video!
I just encountered a problem. The mouse sensitivity gets reeeeaaally fast every time, I look at my grass. I found out that this is because of the FPS. To solve this problem, you have to set the FPS to a amount, so it cant change. You can do it like this: (Paste this into start method)
public int targetFrameRate = 60;
// Set the target frame rate
Application.targetFrameRate = targetFrameRate;
// Set the fixed delta time to ensure consistent physics calculations
Time.fixedDeltaTime = 1f / targetFrameRate;
Hey @MikeSchvedov i keep getting an error that says the file name and class name doesn't match. How do i fix this? (Btw this tutorial is absolutely amazing thank you so much)
Its some bug in unity.
Just delete the script, and create a new one.
It happens when we create a script but press enter instead of giving it a name right away.
Most importantly make sure that when you give the script a name, it stays the same inside the script (Class name).
@@Mikes-Code Thank you!
NullReferenceException: Object reference not set to an instance of an object
SelectionManager.Update () (at Assets/Scripts/SelectionManager.cs:19) i have this Problem please tell me How can fix that ?
broooo i had the same problem but i managed to figure it out, what i had to do was take off text mesh pro. it has to be regular text, it probably is way to late but hey, i tried.
"InteractableObject" not found Solutions within the SelectionManager code?
You need to create a separate script, call it InteractableObject and place the code in the description in it.
When trying to get the object name by looking at an object I get this error: "NullReferenceException: Object reference not set to an instance of an object
SelectionManager.Update () (at Assets/Scripts/SelectionManager.cs:28)" Does anyone know how to fix this? I've been trying but I just can't figure it out.
Hey,
It says that the problem is on line number 28 of the SelectionManager.
What are the objects you have on this line?
One of them is missing a reference.
Can you copy and paste the code of this line over here?
Maybe I can help, although its usualy hard without being in front unity itself.
@@Mikes-Code interaction_text.text = selectionTransform.GetComponent().GetItemName();
@@Evelyn-dh4zp It looks like when you point on an item, this line is trying to access the "Interactable" script on the item.
But if you are pointing at something that is without this script - It will cause a NullReferenceException like you have.
I don't remember at what point we changed the code but there should be an IF statement surrounding this line, and in that IF statement we check first, if the selectionTransform.GetComponent() is not null, only then run the code.
It should be something like :
var selectionTransform = hit.transform;
InteractableObject interactable = selectionTransform.GetComponent();
if (interactable) // basically if its not null
{
interaction_text.text = interactable.GetItemName();
}
this code should be here at some point in the tutorial.
I just don't know where in the tutorial did you reach.
@@Mikes-Code But this shows up only when I point at the trees which have the interactable objects script and have the item name Tree
I paused the video at 18:04 after you pointed at the trees and it worked for you but not for me despite me doing everything that you did in the video up to that point.
yes i am having the same problem when i look at the tree it just logs the game out
9:15 i get this error when i import the script Assets\Scripts\SelectionManager.cs(3,33): error CS0246: The type or namespace name 'MonoBehaviour' could not be found (are you missing a using directive or an assembly reference?)
here is the updated script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SelectionManager : MonoBehaviour
{
private TextMeshProUGUI textMeshPro;
public GameObject interaction_Info_UI;
TextMeshProUGUI interaction_text;
private void Start()
{
textMeshPro = GetComponent();
interaction_text = interaction_Info_UI.GetComponent();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent())
{
interaction_text.text = selectionTransform.GetComponent().GetItemName();
interaction_Info_UI.SetActive(true);
}
else
{
interaction_Info_UI.SetActive(false);
}
}
}
}
Did you find out how (im having the same problem)
To raycast to the center of the screen use:
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
Instead of;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
The mouse will not always be at the center of the screen.
In the previous tutorials he locked the cursor to the center, would this still be a problem?
@@eeni14 No
@@eeni14 Yeah, it still was, guess somewhy even with locking cursor mouse position can be offset from the center, I struggled with it.
i agree with @SP261280. This solved my problems with displaying text on screen.
Thanx for amazing tutorial.But i have some problems.When i hit play my character look down at start.And rabbit starts walk and when it stops,it rotates 90 degress and goes idle with its head inside terrain.Then rabbit starts walking again and turns normal.When i hit play and look the game in scene window, my chracters cylender starts game 90 degrees rotated faced to ground, and the when the rabbit ends walking,it is also rotated 90 degrees.When i look inspector, both objects rotation values never change.
@Mike's Code where is the Interactable Object script?
thanks ,
i have problem about rabbit , in inspector i don't have box cellinder and rigidbody , then in Hierarchy show 2 side box blue one side black
Is it not way easier and scaleable to use layers to distinguish between interactable and non-interactable gameobject? you even used this approuch previously for detecting if the player is on the the ground.
The physics methods even take in layermask object as params so i would asume this is the way to go no?
On my script i dont have the part where you add text
My bunny keeps sinking about 15 less that the normal height and then it goes in the same height in which ever direction, floating bunny. I tried the rigidbody but that did not help much, any suggestions?
Amazing tutorials first of all! I have one small problem so far. My Rabbit moves and all that good stuff but wont HOP! driving me crazy! Anyone else?
I fixed it the Hopping!
@@xdiggidystaticx1596 How, I am having the same problem?
My rabbits flips after run animation and starts floating if colides with another colider :P
when i place crosshair, ITS PIXELIZED AND LOOKS TERRIBLE, even the unity presets looks horibly pixelized, i tried big resolution images, small, different formats, all the canvas settings, image settings in unity, nothing changes the pixalized terrible look of my crosshair white center dot... I am already trying to fix it for 2 hours and i cant figure out whats the problem..........
but if i use text: + ("plus"), it looks normal
nope EVEN THE TEXT IS PIXELIZED......
am I the only one that the selection manager script doesn't work? It keeps giving me errors when I paste the script.
We can also limit the distance for ray
Please help! The select manager is not working I’m using the old text.
Thank you
Can you help me pls?
My rabbit animation is not working. I tried to put them as in the video but still its not working
Anyone else get the error: The type or namespace name could not be found (are you missing a using directive or an assembly reference?) with the ? In the SelectionManager script?
nvm, it didnt recognise the class because i didnt have the script for it yet. Fyi, get the InteractableObject script at the same time you get the SelectionManager script, then you won't get the error.
@@nickhermanns4368 Thank you man i had the same issue
Hi Mike, can you help me, if for the third person point of view the coding script part is the same as in the video or something has been changed and I direct it to the object, the writing doesn't appear like the one in the video because I use the third person point of view, is there a solution? Thank you
my rabbit prefabs are clipping inside the terrain any solutions? @Mike's Code
Can somebody help me with the missing text when i point my cursor at a tree?
I’m getting the error (interactableObject could not be found (are you missing a using directive or an assembly reference?)) any help?
make sure your InteractableObject script name is same as you used in the Selection Manager Script
Any fix for some sort of lag spike or frame rate drop when hovering over game objects?
Can't get the public GameObject to show in the inspector.
Assets\Scripts\SelectionManager.cs(28,73): error CS0246: The type or namespace name 'InteractableObject' could not be found (are you missing a using directive or an assembly reference?)
how do i fix this??
Tix, you need to add a script for the InteractableObject. This isn't included in the description, but it is referenced in the video at 14:13.
@@barrysrandomness thank you for trying to help, but figured it out
@@Tix_Blox how?
This Interaction Info UI its doesnt there in this Script :(
Same
same
@@Warp_Speed_Studios how i can fix that?
@@lelufti i think you create the InteractableObject C# script
@@Warp_Speed_Studios No! i make it 100% who Mike in his Video
Can I make an rotation animation because it just teleports to the other way
Hi Ik this is an old video but I can’t seem to get rid of the error (10,5) The type or namespace ‘Text’ could not be found
Are you using TextMeshPro?
If the camera fails to find the target!
Set the camera as tag main camera !
void Update()
{
Ray ray;
if (Camera.main != null)
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
}
else
{
Debug.LogError("Main camera is not found!"); // error log
return; // code stop
}
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent())
{
interaction_text.text = selectionTransform.GetComponent().GetItemName();
Interaction_info_UI.SetActive(true);
}
else
{
Interaction_info_UI.SetActive(false);
}
}
}
}
Tysm
hello Mike, great tutorial, I find myself stuck in the third tutorial,minute 9:04, i try to drag the script c# SelectionManager into the empty object of SelectionManager and the new Component is lacking one section that appears in your video, its called Reference to the object, this one doesnt appear in mine. I checked so many times the spelling and reset the scripts, erase them and made them again, restart the project and nothing, i tried all i can, will it still work without it?
If you mean the Interaction _Info_UI where he drags the text over it's because the script doesn't compile. If the script doesn't compile, the little box that's attached to the variable public GameObject interaction_Info_UI doesn't show up. I had to comment out some of the code that refereed to the InteractableObject. Drag and drop the ui text then un-comment the text again. Hope this helps. I haven't got past 9:23 yet. (sleepy time) - Also use TextMeshPro not a plain text.
hi i want some help i have this problim in selection manger
{ NullReferenceException: Object reference not set to an instance of an object
SelectionManager.Update () (at Assets/Surivival/UI/scripts/SelectionManager.cs:1 }
this is code 18
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
can any one help pleas
Me too
hello there
i am having a problem in the SelectionManager Script
Firstly the pastebin link isnt working
secondly when you drag the interaction_info_UI script to selection manager i am only able to see 1 option which is 'script' where altho we cant edit it but as in your video i am not able to see the refrence section where i can drag the interaction_info_UI.
i tried many times tried typing the scrpit by myself, coping scripts from comments, using TextMeshProGUI,new project everything but i am stuck there
hope you understand the problem and help me out!!
Well the pastbin link is working, I just tried it right now.
Try opening it with another browser, maybe some browsers block the website or something.
About the missiong option in the inspector, make sure that all properties are "public" and not "private" this will make them visible in the inspector.
Following the tutorial should give the same results, but it's easy to miss something that can mess things up.
@@Mikes-Codethanks you so much mike sir,
if will comment again on your video if anything is needed thank you for your help
Great tutorial! My bunny appears for a split second when I first play my game, and then immediately disappears! I'm not sure what I'm doing wrong hah.
I figured it out. My rabbit was falling through the world. Once I spawned the rabbit a bit higher above the terrain, it was there bouncing around being cute :D
Hi Mike, I have a question for the selection manager script because it says “the type or namespace“interaction-able object” could not be found” pls help
You need the InteractableObject Script to be "at least" in your Project folder for the selection manager to detect it and eventually attached to an object as a component for it to display the name. The code is on the description of the video.
@@robertorodriguez4304 Thank you for your enthusiasm, bro. It saves my day.
error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)
How to Fix Help me
You simply need to add the UI namespace.
Hover over the Text, click on "show potential fixes", and then click on "using UnityEngine.UI".
or simply add "using UnityEngine.UI" at the top of the script
Bro i was facing the same same problem but here is solution it worked for me
it is updated script for TextMeshPro Unity Version
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SelectionManager : MonoBehaviour
{
private TextMeshProUGUI textMeshPro;
public GameObject interaction_Info_UI;
TextMeshProUGUI interaction_text;
private void Start()
{
textMeshPro = GetComponent();
interaction_text = interaction_Info_UI.GetComponent();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent())
{
interaction_text.text = selectionTransform.GetComponent().GetItemName();
interaction_Info_UI.SetActive(true);
}
else
{
interaction_Info_UI.SetActive(false);
}
}
}
}
Hello, Can you make a story dialogue tutorial? I hope you consider it since I need it for my capstone project.
Pay him a couple thousand or do it yourself, nobody is going to do your work son.
@@smokinjoe9415 did I do something to offend you? I'm not talking to you idiot. unless the owner of this video/tutorial decides it to my comment, not you. If he rejects it that's fine with me.
How did you get art, then ui art, then sphere? They weren't there for me, and I created art and then ui art in that, but how do i get the sphere? Also wondering how you had the rabbits in the hierarchy area
Bro you are using different unity version i think
it because he made those folders him self
It all works after i got a bunch of errors and tried again but my fps got 6 times worse
nevermind. i resetted the game and it started workin again
[SOLUTION IN REPLY] Getting an object reference nullreference exception error:
NullReferenceException: Object reference not set to an instance of an object
SelectionManager.Update () (at Assets/Scripts/SelectionManager.cs:19)
this is line 19: Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
pls help
Hey for anyone else having this issue I fixed it!
It was because I deleted my main camera, to fix this make a new camera and then set the tag to MainCamera in inspector!
@@Fortkitty Should I rename my main camera to MainCamera ? I dont understand it would you help me ?
I found it thx, it means a lot to me :)
thanks man, been pulling my hair out for fucking hours over this lol
@@Fortkittythank u!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Bit of help here-
Assets\Scripts\SelectionManager.cs(9,5): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)
change all Text references to TextMeshProUGUI
@@sethluca that’s not working anything else I can try
@@mikeyororke9114 add: using UnityEngine.UI; to the top of the selection manager script
When I tried to use the SelectionManager script it said "interactableObject" was missing.
Any idea how to fix this?(all the other scripts worked fine though)
Any help or a copy/paste script would be appreciated, thanks in advance.
You need the InteractableObject Script to be "at least" in your Project folder for the selection manager to detect it and eventually attached to an object as a component for it to display the name. The code is on the description of the video.
@@robertorodriguez4304OMG TYSM!!!
I didn't wanna go farther in the video until I figured it out XD
Though I should have realized, again, tsym
hey i am confused on how i can do the SelectionManager scripts. I don't know how to do it
If you are having trouble with the selection script you first also have to have the interactable object scripts
@@Leaks_the_vegetable oh ok thanks!
hi im getting this error with selection manager can anyone help please?
Assets\Scripts\SelectionManager.cs(6,33): error CS0246: The type or namespace name 'MonoBehaviour' could not be found (are you missing a using directive or an assembly reference?)
It seems like you are not importing unity engine in your code. Add this on the beginning of the SelectionManager.cs :
Using UnityEngine;
Here is updated script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SelectionManager : MonoBehaviour
{
private TextMeshProUGUI textMeshPro;
public GameObject interaction_Info_UI;
TextMeshProUGUI interaction_text;
private void Start()
{
textMeshPro = GetComponent();
interaction_text = interaction_Info_UI.GetComponent();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent())
{
interaction_text.text = selectionTransform.GetComponent().GetItemName();
interaction_Info_UI.SetActive(true);
}
else
{
interaction_Info_UI.SetActive(false);
}
}
}
}
when they stop they hit their head and they fall and sometimes they are floating
Box collider very big.
PLS HELP GUYS!!!!!!!!!!!!!!!!!!!!
I have a problem with the script, mine says:
Assets\Scripts\SelectionManager.cs(25,49): error CS0246: The type or namespace name 'InteractableObject' could not be found (are you missing a using directive or an assembly reference?)
how do i fix this?
you have to put an extra script into the object likes trees he mention this later in the video but have not uploadet the script. so look full video then try it again.
Ok i Will try that, thx!
l really need help when l klick right butten and go to "UI" l dont have "text" can someone tell me why
Bro because we are using different unity version there is textMeshPro if you want script for textMeshPro i can provide you ❤
Here is updated script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SelectionManager : MonoBehaviour
{
private TextMeshProUGUI textMeshPro;
public GameObject interaction_Info_UI;
TextMeshProUGUI interaction_text;
private void Start()
{
textMeshPro = GetComponent();
interaction_text = interaction_Info_UI.GetComponent();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent())
{
interaction_text.text = selectionTransform.GetComponent().GetItemName();
interaction_Info_UI.SetActive(true);
}
else
{
interaction_Info_UI.SetActive(false);
}
}
}
}
Can anyone please write me the whole code of intractable objects
link at desription