Unity Tutorials For Beginners | Passing Score Health Between Scenes + [BONUS] How To Spawn Player

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone Terry from introverted games here and today we're going to go over two main Concepts in this video the first concept is how in the hay do I persist data from scene to scene meaning I have a score and it's working great in level one but when I go to level two it's not working anymore how do I fix that all right I'm going to show you how to do that and the other thing I'm going to show you how to do is how to instantiate or spawn your player into each scene as you change there's many ways to do this and I get asked this question a lot should I keep a new copy of my player in every scene should I spawn my player in what should I do well I'm going to show you a solution for that as well so without further Ado let's get cracking I suppose the best place to start is to show a common problem that a lot of indie game developers have either had in the past or they currently have you have this beautiful game that you've built you're playing around everything's working you've got this little UI you know your scores adding up everything looks great and then you hit your portal to your next level and boom your score is gone you're defeated you're like what the hey y'all you want to throw the towel in you're irritated right well don't worry the fix is really really really really simple when you see this you're probably gonna scream all right let me show you how to do this we're gonna go into our scripts folder or wherever you keep your scripts I don't judge they could be in your backpack for all I know right okay we're gonna make a new c-sharp script and I'm going to call it game data you can call this script anything you like this is your game it's your private universe who am I to tell you what to name your script you can call it Pink Panther I don't care but just know that this script is going to handle saved data now you're probably used to making all mono behaviors but this script is not going to be a mono Behavior we're not going to inherit from the unity engine or any of that business this is just a c-sharp script and we're going to make it a public static class called game data now inside of this public static class we need to make a public static integer but this could be a vector 3 a float a string you can save any data in here you like this is just for intents and purposes of this tutorial so public static int and we're going to call it heck score right that works for me and we need to do a git and a set now if you don't know what any of this means let me explain it to you real quick public static class can be accessed from anywhere now let me show you the difference if I can find one okay well you know right here we're doing git component gosh it's so hard to find an example when you want to but just suffice to say that this way we don't have to get a reference okay we we automatically always can access this script from anywhere in the game without first getting a reference and the same thing goes for this public static integer of score do not make this private because you won't be able to access it and this won't work now believe it or not the save system is done this is it this is all you need you could of course add more things like you know uh public strength oops string oh public static string oops and we could call this player name and we could do a get in a set you see you see what I'm saying here but your save system is finished well your poor man's database is finished this is this is a great way all right now what we're going to do is go over to UI score this is just the script I set up that handles the visual representation of the data that we're managing uh in layman's terms it adds a number on the canvas when you get more score when you collect an item it increments up so we're gonna go we're gonna make a start function to begin with because we don't have that yet okay and down here we are going to save some data now are you ready to have your mind blown watch how easy it is to save data with this system you this is fantastic okay please give me a like and a subscribe you want to see more videos like this for sure okay we're gonna go game data because that is the public static class that we made we're going to go Dot and we're going to say score and we're going to increment it based on the incoming integer that we have for the function and you don't want to do equals guys if you put an equals here then your score is always going to be like 10. it'll never increment up you have to add so plus equals will add this to the score every time this function is run it will add more to the score capiche okay now there's one more thing we want to do because having the score is fine and dandy but we have to see the score we're humans we're visual creatures right this is very simple you're going to hate me probably so score text Dot text and that's just the text mesh Pro object I have in my canvas and we're going to say equals game data not game object come on now work with me here gamedata DOT score and um we do want to go to string and that's because while it may work you don't really want to put a string value into an integer slot or an integer value into a string slot that's akin to trying to cram a scrounge a square peg into a round hole now you hit it enough times it's going to go in but it's not a good fit and you really should just put the round peg in the round hole meaning put the string data into the string slot okay there we go now that is done we now have data that persists in between scenes it's that easy that's all you have to do let me show you with a brief demonstration of our beautiful caveman game here in all reality I made this game in about dead two hours or so all right we're gonna run over here we're gonna collect some stuff boom boom boom 20 30. we're doing great getting all the meats there's 40. jump down here hop onto our little teleporter boom and look at that we have 40 points in our score and we're gonna do it again uh 67 let me hop on the teleporter and we have 70. isn't that fantastic how beautiful that is so easy now I do want to show you one more thing now if I hit stop you'll see there's no player in the scene there's several ways to tackle this problem yes you can put a prefab of your player in every scene if you want to I prefer to instantiate or spawn my player into every scene as I go to each scene let me kind of explain that to you a little bit here in my player start point I have a player spawn point all this is is an empty game object you see there's nothing on it it's just empty and what I want it for is the transform this is the position that I'm going to spawn my player in in the scene so wherever I put that that's where my player is going to spawn in on this particular scene when this scene starts now I have a level manager I probably shouldn't have put the players spawning in the level manager whatever this is a beginner tutorial okay now I have a spot here for player prefab and a spot here for spawn position now also I use sin machine or sign machine cinemachine whatever for my camera it's a fantastic camera you don't have to use it but if you do you're going to want to spot for that too and you're also going to want an integer for what scene to load now I'm going to show you this script I'm going to show you how it works because if you ask me this is nice to have so level manager you see here I I put tool tips in my scripts you do whatever you like but in my opinion tool tips are nice these are all those things I was telling you about in the inspector now on enable we're just subscribing to when the player dies so that's not really pertinent for this uh tutorial okay but my level trigger there is an event there called win condition and that's how I'm triggering My Level manager to change levels that's also not important for what we're talking about what is important is the start function now you see here I have a vector 3 I've called a position and I've given it our spawn position dot position spawn position is just a transform that we drug that we drag we Dragged In from the inspector okay now the reason I introduced a variable here is so I don't have to put spawn position dot X spawn position dot y this is more performant okay so I say that this Vector 3 equals spawn position dot position and then I say I make a new Vector 3 called spawn location and I'm assigning it the X Y and Z values of my transform my drag I drug in now this is important you have to make a new vector 3. don't ask me why this you just have to okay and then what we do is we say VAR player which is a game object now here I use VAR you may not use VAR let me show you how this is done the other way around just so you don't get confused I'll show you both ways so we could say um actually no I can't I can't I don't even know how to do it the other way anymore it's been so long but this code Works VAR is performant VAR works and really it's just saying this is a vector three so I guess I could say new Vector three test oh yeah yeah that's how you do that's how you do it you go Vector three test equals new Vector three so when I say VAR spawn location equals new Vector three it's the same thing as saying Vector 3 test equals new Vector three so instead of saying you know it's the same thing if you're not understanding it's the same thing with the player here so instead of saying VAR player game I could say um game object player equals new game object okay so if you don't understand my code that's just what it means it's just written differently so let's stop ranting and get back to it so I have my player game object and I'm telling it this game object equals instantiate player prefab now player prefab is this game object right here the player prefab you want to spawn okay now spawn location is of course the location and then quaternion.identity just tells it to use the current rotation of the prefab it works this way you don't have to set any oil or angles or anything this just works okay and then because I use send machine I have to tell it playercamera.follow this transform otherwise the camera won't fall on my player I hope that you've followed along with this I'm trying to just explain how it works this is how I spawn my players in in every scene it's it's better this way if you ask me and this way you don't need say a don't destroy on load and blah blah this is just easier so feel free and copy the code if you want it works really well and that's an added bonus I'm going to give you in this video all right guys that's pretty much everything uh it's on YouNow go out there and make something beautiful if you have any comments if you totally hate my videos if you have a suggestion for another video drop me something down below I appreciate you guys until next time happy coding
Info
Channel: Terry C Indie Game Dev
Views: 1,001
Rating: undefined out of 5
Keywords: unity tutorials for beginners, unity tutorials for begginers, unity tutorial for beginners, unity tutorial for beginners 2022, unity tutorial for beginners 2d, unity tutorial for beginners 3d, unity tutorial for beginners in hindi, unity tutorial for beginners - c# coding, unity tutorial for beginners 2021, unity tutorial for beginners brackeys, unity tutorial for beginners 2023, unity tutorial for beginners playlist, Unity tutorial for beginners 2d game
Id: 8xWKyePYtW4
Channel Id: undefined
Length: 14min 52sec (892 seconds)
Published: Sun Feb 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.