Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Banana cold is getting a little bit more complicated
You may start to notice something called redundancy
We've found the player here
Also found him here
We found the component here and we've also found here
So we basically done the same thing twice when we should only do it once
Why can't we have a reference
To the player in the new player component only once so they have to do this over and over and over again
Wine this is a lot of typing so for example if I wanted to do a lot of different things with the new
Describe so what if ontriggerenter I also wanted to
No maybe set the m o as well I have to go game object that fine player oh my goodness
Takes forever I don't like coding I hate my life we don't want to do this that's just a mess
So
Subtitled by -♪ online-courses.club ♪- We compress knowledge for you! https://t.me/joinchat/ailxpXoW3JVjYzQ1
There's two ways that we can solve this
First way is to create a reference
The player
So we can do this
Like this we can say
New player
Is
Cold
New player
Is a = game object
Find new player
Getcomponent
New player not our doing the same as acting
We're doing here
That's what new player is
Then are we going to do is go and replace this to new player
And then
New player
What we just done is we've created just like we do AP remember we can create variables up here before the
Function
What we done is actually created a
Variable inside of a function
So this new player variable
We just created it
Reference it below whenever we want we can do this at any time because a new player diablo
Equals in
New player that transformed opposition equals new
Hector 30413 position into the zero position in another why we want to do that but
We could reference new player over here
Ok
Here's the problem
What this is this is a local variable
And it's local to this function
So it because it inside of this function
I can actually reference anywhere else I can't reference it in the update so I can say new player
Dart
Coinseed like it just screwed it up
It doesn't exist inside of the update
The reason why most your variables should be declared
Outside of your functions is so that those functions can use them
So because these functions are born outside of the start function outside of the update function in outside
Dysfunction
Is that we can reference these wherever we want
So what we can do here is take this
Delete it
Pasted up here
The problem is you can actually use gameobject that find
Outside of a function rate so we actually can just go new player is new player will it delete that
So we prayed in a new player variable
And it's of the class new player
Pain in side of the start we can actually find it
So new player
Equals
Find new player that get component new player
Now
Remove that really quickly on you that
The animal is well here
So now we can reference new player all over the place
Reference the inside of the update
We need to have a semicolon here
Errigal
Leaving reference it inside of the update
New player
The notice now we can do we can change new player that ammo and we can make it 1000 if we went inside of the up to
Sunshine wicked change the players
Coins if you want to coins collected
Equals 9
Change it inside of the update function and we also change inside of the ontriggerenter function
We can use this new player reference all over the place
Inside of a collectible script
As a recap guys
You're trying to avoid redundancy and that is using lines of code over
Over again that are identical
The Look
We created a variable
In it's not a bully and it's not integers on a float in this case this variable is a
Glass
New player
Today's one single line of code
Doesn't allow unity to actually know which new player class with referencing
Well
We're actually referencing the one on the new player
So that's why
When we start the game number when the start function here
The moment we start the game
Immediately we want the new player variable here
To find the component that were actually trying to reference
So it's going to say new player equals
A magical find new player so I can find the game object
And then I cannot find the component
New player
Now you need a nose that's the one
That we want to reference
No because we found that component
We now know
Any time we use this variable it equals this
So we can use it all over the place
The moment our character touches or triggers into
This collectible
It's going to be right the coins collected up one
Is also going to fire the update you a function that's inside of new player
Instead of writing this over and over and over again we can just have a reference a single reference to it
And use that single word
Over and over again
This it makes sense to you
We're gonna keep going and it's going to slowly start making sense more and more
As we use this as often as we can
Again anytime you see a line of code that identical
You really want one reference to that line of code if you can
So in conclusion guys if you ever are using a line of code over and over again so in this case
It was this line here we were using that multiple times
Accuracy that that's called redundancy and you really don't want to do there it makes it painful for you makes
Enabled for your team because you've got a cold it's a written over and over and over again
Also slows down unity every time you find a game object and then find a component
It's actually slowing down unity just a little bit
So if you make a single reference right here
When we fire the start function the moment we start unity
Unity nose immediately what new player is
Now I can actually use their variable
To collect coins and update the UI
Save it out and just checking to see if it works
When I get a worry about this morning's here guys you going to see a lot of warnings when you're making your game
It's good to try and get rid of them but I'm just gonna clear I'm out for now
Alright so looks like we have
Strange figure out what this area is
Is object reference not set to an instance of an object
What is the price of an easy solution with double click on it and see what happens
Ok looks like it can't find new player
Looks like we've got a little bit here about in the future how you can avoid these kinds of mistakes
Game object that finds new player will remember we don't actually have a game object called new player
Gameobject has just called
Player
As you can see it unity can't find that game object
It's going to tell you
So we need to do is make this player
It said
Jump back in the Unity
And let it play in see how it looks
Alright it's like everything is working
Awesome
That guy is another way that we can tell unity
Who new player is
And also which component with referencing
We can actually remove this completely
So all we have is this new player
Variable that doesn't actually reference anything
Holidays is Ava type new player
Have you got back in the Unity
Universe we don't get any areas around
That's because unity is thinking well
Maybe Thomas is going to tell me in a different way
How exactly could we do that
Go ahead and select this little coin here
Like this coin
And you noticed that the new player variable isn't showing up inside of the component
What's open up the collectible scrapyard
What's go ahead in serialise this
Serializefield
Ennis pizza ring make it private
Go back in the unity here
And you can see
The Honor collectible script now we have a new player
Ok
So now we can see the new player well you can see that it's set to 9 it doesn't know which new player were actually Reverend
Well we can actually while it's selected
Just drag the player
Over 2 this field is automatically going to
Find
Subtitled by -♪ online-courses.club ♪- We compress knowledge for you! https://t.me/joinchat/ailxpXoW3JVjYzQ1
So
If we had play here
You noticed that
This one here
Actually knows who the new player is
So we can actually collect it you'll fire that update you a function in there we go
These
They don't know you can see we have an error
I don't know because we didn't actually
Rag
A player over to this field here
So that is why
In our case
We actually white
To include
This
For every single collectible script
Sometimes you gonna want to actually find it in The Script
Are the times
Especially if you only using a gameobject 1 so for example if I was referencing something from the player script
Actually just drag coins text over here
Because I'm going to have one player in the entire game
Dragging the coins text over here
Actually makes a lot of sense
So we going to have a new player script
We can actually just get rid of this
Now
Is there a finding that in the strip
Just told unity with a simple drag-and-drop
Where are coming to the Pony actually is
If this doesn't make sense guys we're gonna be doing this a lot in these tutorials
So don't stress too much I just keep going
Hopefully you'll understand when you want actually read out the script and maybe when you want to drag and drop a good rule of thumb
If multiple objects say enemies enemies hundreds of enemies
Are referencing one game object
Do you want to do that in the Script
OK mate that's fine
Getcomponent
If on the other hand a single game object for example this player
Is referencing another single game object so in this case
The coins UI element
I see no reason why not just drag it over
Infidelity
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.