Unity 3D Instantiating GameObjects & Prefabs - (In 2 Minutes!!)

Unity 3D Instantiating GameObjects & Prefabs - (In 2 Minutes!!)

Royal Skies

3 года назад

7,019 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Royal Skies
Royal Skies - 25.03.2021 01:55

This code really helped me back when I started. I'm sure there are ways to improve it, but this little guy does quite a lot, hope it helps :)
{
public GameObject deathExplosion;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "environment")
{
GameObject deathObj = Instantiate(deathExplosion, transform.position, transform.rotation) as GameObject;
Explosion deathScript = deathObj.GetComponent<Explosion>();
deathScript.sizeGoal = 80;
Destroy(gameObject);
}
}
}

Ответить
Santos Rasputin
Santos Rasputin - 09.08.2023 04:57

Guys, anyone good at code? even chatgpt cant get me out of this one, i dont want to drag the prefab to a open variable of GameObject in the inspector, i want to do it by code, the prefab is in Assets/Prefabs/GoHadouken.prefab....any clue?

goHadoukenPrefab = (GameObject)Resources.Load("Assets/Prefabs/GoHadouken", typeof(GameObject)); this isnt working at all....any help is welcome

Ответить
Devlog with Jake
Devlog with Jake - 08.08.2022 23:55

Good video, simple and straightforward. Thanks!

Ответить
Dragan Kordić
Dragan Kordić - 07.10.2021 18:38

Thank you very much, you helped me a lot. I am a beginner and you really told me exactly what I needed in the first sentence. You are great, thank you again.And so many of them are blabbering on,on You Tube- without saying the essence, I could hardly find the simple thing I needed. How effects prefab to put in the code! .. So easy and so complicated to find. Again, thank you !!

Ответить
masonmason22
masonmason22 - 29.03.2021 03:42

Some of the best unity tutorials I've ever seen. Thanks man.

Ответить
AllEx
AllEx - 28.03.2021 21:03

Why are you using cast?

Ответить
Rei das Laranjas
Rei das Laranjas - 26.03.2021 22:46

Thank you isn't enough for what your channel has been make for all this time

Ответить
Namir Reza
Namir Reza - 26.03.2021 12:38

The only channel i forgot to comment

Ответить
Thalpius
Thalpius - 26.03.2021 03:39

You should make a tutorial on how to play noises

Ответить
David Boura
David Boura - 25.03.2021 23:27

Merci.

Ответить
RestushLogic
RestushLogic - 25.03.2021 11:36

Addressable and aaset reference save memory!

Ответить
Kriegs
Kriegs - 25.03.2021 10:11

yes

Ответить
Risuniku
Risuniku - 25.03.2021 03:30

I'm one whole hour late :( I'll do better next time

Ответить
0% Pride
0% Pride - 25.03.2021 03:25

Been watching you for a few months now. You have the best teaching style out of anyone I have seen on this platform (and Udemy). I appreciate the energy and the short and easy to follow explanations (and not making me beat my head on the table waiting for the info to stick). Especially for an intermediate 3d designer/ game developer.

I'll be back every time:)

Ответить
Jason Williams
Jason Williams - 25.03.2021 03:03

Thanks for teaching us! If I make it big I will not forget

Ответить
Reuben Sandwich
Reuben Sandwich - 25.03.2021 03:01

Outro song is super catchy. Is it your composition?

Ответить
Truce Feud
Truce Feud - 25.03.2021 02:52

Man, this really helps me figure out how to achieve my animation for leveling up. I was a bit confused about how I was going to tackle it, but now I'm absolutely pumped. THANK YOU!

Ответить
ktmochiiidev
ktmochiiidev - 25.03.2021 02:48

Hey royalskies are u ever thinking of doing a vid on playmaker someday? (It's a visual scripting asset for non-coders).
Seems like it's useful for only simple stuff but it looks kinda interesting.

Ответить
Alisher Sadullaev
Alisher Sadullaev - 25.03.2021 02:27

useful

Ответить
Mason Wheeler
Mason Wheeler - 25.03.2021 02:18

If you notice, Visual Studio faded out the "as GameObject" in your code. This is because Instantiate is a generic method, and will automatically set its return type to the same type as the declared type of the thing being instantiated. So if you're instantiating a variable declared as a GameObject, then the return type will be GameObject already, so there's no need to cast it to GameObject.

Ответить
Ichigo
Ichigo - 25.03.2021 02:17

You can make the code much cleaner by putting all the garbage in an static method:

public static void ExplodeObject(GameObject prefab, Transform t, float size = 20.0f)
{
var obj = Instantiate(prefab, t.position, t.rotation) as GameObject;
var exp = obj.GetComponent<Explosion>();
if(exp == null)
throw new Exception("Blah, blah, blah, incorrect prefab or something :|");
exp.sizeGoal = size;
Destroy(t.gameObject);
}


And you'd use it like so:

void OnTriggerEnter(Collider c) // c instead of other because the name of the variable doesn't matter, the type matters.
{
if(c.gameObject.CompareTag("environment")) // I'm starting think you're using string comparison instead of tag comparison on purpose...
Explosion.ExplodeObject(deathExplosion, transform, 80.0f);
}


This essentially does the exact same thing, plus check that the prefab's root object does indeed have an Explosion component, but it's much cleaner, there isn't a way to make everything cleaner here, not that I can think of off the top of my head, but moving all the clutter from the Player class into the Explosion class, we're hiding it underneath something we're rarely gonna touch after the matter if at all.

Something else I wanted to say is that it may be worth doing a very basic tutorial on doing custom inspectors, since this is something that would benefit from it, in that you can expose the deathExplosion variable, but before assigning it to whatever the user dragged or whatever, you can then check if it has an Explosion component, and if not just do nothing, otherwise set it as you'd normally do, you can also make the inspector pretty, put things in tabs with toolbars and stuff! And it's actually quite a bit simpler than you'd think, I can provide a couple examples if anyone wants it, otherwise it can be looked up on internet, there's plenty tutorials and articles on it.

Ответить
Ikxi - Forever a Tatsunoko
Ikxi - Forever a Tatsunoko - 25.03.2021 02:11

Ответить
Ken Higuchi
Ken Higuchi - 25.03.2021 02:11

Second!

Ответить
WizZzLe
WizZzLe - 25.03.2021 02:03

Hi

Ответить