Quick Tip: Referencing Assets through Code | Unity Tutorial

Quick Tip: Referencing Assets through Code | Unity Tutorial

Code Monkey

6 лет назад

51,792 Просмотров

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


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

RickyFreddy
RickyFreddy - 29.08.2023 09:44

Loving this method. I usually have a UIManager, GameManager etc where I drag assets and have the logic but from now on I will make "UIAssets" etc. to make it cleaner and easier to find where I have stored references. 😊

Ответить
Reut Tal
Reut Tal - 21.08.2023 23:34

Does this work in builds?

Ответить
B8_Why
B8_Why - 26.06.2023 14:04

Hi CM, How do I get Code Monkey Heat for reference?

Ответить
Tiagocf2
Tiagocf2 - 06.02.2023 21:08

why is that so overcomplicated lol

Ответить
aswhy
aswhy - 20.01.2023 02:21

I tired adding the Game assets script to the prefab and it just keeps telling me the script class can not be found do I need to add the game logic script as well?

Ответить
InsaneCliche
InsaneCliche - 13.01.2023 17:12

I know this video was made a while ago but could someone explain the purpose of doing this? Like what problem it solves?

Ответить
Marjut AK
Marjut AK - 03.01.2023 20:18

Hi! I am not sure if you still checks comments in older videos, but I am not able get the GameAsset prefab to show in the hierarchy. I do not also get an error or anything which makes me think I am not using the GameLogic script correctly? Am I supposed to call it somewhere? I have watched the video over and over again and everything else seems to be in right places. Thanks!

Ответить
Poppy Cao
Poppy Cao - 03.12.2022 09:19

Hi,I got a null reference: NullReferenceException: Object reference not set to an instance of an object
ChatBubble.Setup (ChatBubble+IconType iconType, System.String text) (at Assets/Script/ChatBubble.cs:48)
ChatBubble.Create (UnityEngine.Transform parent, UnityEngine.Vector3 localPosition, ChatBubble+IconType iconType, System.String text) (at Assets/Script/ChatBubble.cs:15)
NpcInteractable.Interact () (at Assets/Script/NpcInteractable.cs:12)
PlayerInteract.Update () (at Assets/Script/PlayerInteract.cs:19)

Ответить
RedReality
RedReality - 14.10.2022 18:27

You completely saved my life. I have to reference my textures in code because Texture.mipMapBias is only accessible that way, and I didn't want to wreck and clutter my project structure putting all my textures in nested Resources folder. I spent the last half day reading docs, forums and Q/As and none of them mentioned you can just have an object in the Resources folder and reference that.

Ответить
as told by adriana
as told by adriana - 08.10.2022 14:33

hey CM! this is an awesome video to help segue into writing more efficient coding~ i just wanted to ask, you've taught us on how to instantiate/create the asset but is there a way of deactivating it as well? let's say i don't want the asset to show up after i'm out of range within something? would really appreciate your help! cheers <3

Ответить
ميبراكو / Myepraco
ميبراكو / Myepraco - 01.09.2022 18:03

This the best thing I could Learn

Ответить
Neo Smooth
Neo Smooth - 15.08.2022 20:18

Hey! Great Video btw.
I havent a Class named UtilsClass ... what is this? ^^

Ответить
GoodNewsJim
GoodNewsJim - 14.06.2022 18:03

Question: When building to a live build, does Unity forget to bring the resource folder in?

Ответить
User Account
User Account - 06.06.2022 04:59

Get rid of those dangles homie those are old time and this is a typed language that has full scoping.

Ответить
Brett Johnson
Brett Johnson - 05.06.2022 20:36

Hmmm, Doesn't work for me.
The prefabs are empty in the instantiated Game Assets (Clone), even after downloading your project files.
"The variable pfDamagePopup of GameAssets has not been assigned." (But they have been.)
Could this be related to project settings?

Ответить
EvanClaire
EvanClaire - 25.05.2022 02:36

great video! this helped me out a TON! i like your method a lot more than the standard method.

Ответить
Vitalii Zurian
Vitalii Zurian - 01.04.2022 15:13

Thank you!

Ответить
neoZoidY
neoZoidY - 07.03.2022 15:39

CM sorry for so many Questions 😓😓 But can you please answer this last on this topic. The Question is why didn't you used a Method for reference instead of Property ???

Ответить
neoZoidY
neoZoidY - 05.03.2022 20:02

Hey CM awesome video , can you explain more about Resources folder ?? about how it is being used and the best practice to use them ? ??

Ответить
Kada Xuanwu
Kada Xuanwu - 22.02.2022 01:25

Isn't this method of instantiating inefficient because unity always has to search the whole Resources folder for every instantiation? (ofc only if you have multiple things there, which I have since i'm using Photon)

Ответить
neoZoidY
neoZoidY - 21.02.2022 09:50

CM why do you use Underscore before variable i ??? By the way awesome workflow method.

Ответить
Brett Abraham
Brett Abraham - 08.02.2022 06:07

For anyone who is getting the "The Object you want to instantiate is null" - Make sure you don't skip the step where he puts the "GameAssets" prefab into the "Resources" folder. That is vital! Hope that saves you a bit of time!

Love this tutorial btw - super helpful!!!

Ответить
NayNay ZA
NayNay ZA - 18.10.2021 22:12

Thank you for this, this has helped me decrease the amounts of times I have had to input UI sounds.

Ответить
alex
alex - 04.10.2021 23:12

Hey Code Monkey! I know it's a bit late and you probably wouldn't respond after 3 years of publishing this video, but I'm getting the following error:
ArgumentException: The Object you want to instantiate is null.
UnityEngine.Object.Instantiate (UnityEngine.Object original)


I checked a couple of times and I think I did everything just as you did it, any clue of what could be happening??

Ответить
JT Lewis
JT Lewis - 26.09.2021 06:35

I'm getting: 'Resources' does not contain a definition for 'Load'. Help, please. I'm trying to use this to go along with another of your tutorials to fire projectiles from a tower. Could it have to do with using a newer version of unity? I made the Resources folder and added a new prefab to it and all that by commenting out the if statement long enough for the scripts to compile, but when I uncomment the if, the error still comes back.

UPDATE: I solved it. I had another script named Resources that I just had to rename.

Ответить
Chillin Games
Chillin Games - 10.08.2021 22:20

i tried this out and its pretty great for sure. some tips i would add is that you can put headers in the script with [Header("Prefabs")] just to organize all the assets in the editor. i also open a second inspector screen and just lock it to this game objects so i can always flick over to it and add the asset as im making them.

Ответить
jaume
jaume - 27.07.2021 21:19

OMG just the exact thing i wanted to know! Also, you think this could run for a ScriptableObject?

Ответить
hakanviajando
hakanviajando - 05.07.2021 01:03

Do you think it's bad for performance or I'm overthinking it, because imagine a lot of assets there and I think it loads all the assets even if they all are used in that scene or not.

Ответить
S2LCheshire
S2LCheshire - 31.05.2021 22:19

Awesome Tutorial! This saves me so much time and code mess. I have a question however, I tried to put some GameObject prefab in this script, but when I tried instantiating it from another script, it gives me "The Object you want to instantiate is null" - it seems like it has something to do with the static nature of it, since I tried putting this same prefab on another (non-static) object and it got instantiated properly. Any thought on this?

Ответить
Megalogue
Megalogue - 13.05.2021 06:03

Hey CodeMonkey, I've been using this technique for a while now, and it's super convenient. I always feel a bit uncomfortable making all those public fields, though. Thoughts on this? I know that in general, it's best practice to make fields private if possible, but obviously the whole idea here is to access these fields from anywhere, so...I'm not sure what the best solution is.

Ответить
ezoray
ezoray - 06.05.2021 02:38

I didn't realise instantiating the prefab was so simple. I was scratching around looking for examples and ended up using AssetDatabase.LoadAssetAtPath ... PrefabUtility.InstantiatePrefab. This method is much simpler, thank you.

Ответить
Matt-The-Slayer
Matt-The-Slayer - 19.02.2021 20:43

I am trying to use this with a Common Mono Library I created that contains common used functions such as a "Destroy After Time" coroutine. However I am encountering an error, "Coroutine couldn't be started because the game object is inactive" after calling it through GameAssets.i. The function does not appear to be at fault as I tested it with the class housing the script directly on the object I wanted to destroy and it worked fine. It appears to be due to how its being used within the GameAssets prefab.

Do you have any recommendations on how to go about this?

Ответить
GOOD GAMER SHOW
GOOD GAMER SHOW - 18.02.2021 10:11

"the object you want to instantiate is null" error occurs

Ответить
N3 iDart
N3 iDart - 15.01.2021 02:17

can'r add scriptr component 'GameAssets' because the script class cannot be found make sure that there are no compile errors and that the file name and class name match.
i have this error but everything i wrote into the script is exactly like yours,the same for folders or folders and fail path, which is the problem? i'm using a unity 2020.1.12f1

Ответить
TehBunnieh
TehBunnieh - 30.11.2020 16:46

I cracked open my head as why it didn't work, it kept giving a null error. In the end I created a folder named Resources to put the GameAssets object in and suddenly it worked. Previously I had put it in a folder which I named Prefabs, . I don't understand why the folder name made all the difference. If by any chance you see this, could you explain to me why the folder had to be specifically named Resources? Does the Resources class require a folder specifically named Resources? I'm a bit new.

Ответить
Hamilton
Hamilton - 16.09.2020 11:06

hey thanks for your tutorials, it's very helpful then . what about " Using Unity engine Codemonkey " in liberary ? how can i get it ?

Ответить
Donald Macleod
Donald Macleod - 29.07.2020 20:31

I hate you! Now I will have to refactor all my code! this is going to make my life so much easier! thanks!

Ответить
Viporal
Viporal - 13.07.2020 02:15

I don't understand. How did anything get called without a start or update function?

Ответить
Iyanu Bosi
Iyanu Bosi - 30.06.2020 23:55

How do I reference a textMeshPro pannel?

Ответить
RedStone
RedStone - 29.06.2020 08:22

O.O, amazing!

Ответить
Sarai Reyes
Sarai Reyes - 01.06.2020 05:12

Hello! I have a question. Can I use this method with any project without using the CodeMonkey.Utils? At the beggining of the script?Thx :)

Ответить
Giorgio Guglielmone
Giorgio Guglielmone - 06.05.2020 20:54

We apologize for my perseverance in asking you many questions about your various tutorials. I find your approach to scripting very interesting, instead of using Mecanim or Animation from Unity sprites. But I would like to understand well the mechanism that you have implemented to load animations via code or data. If you prefer I can give you my email, so we don't occupy this channel, with my questions for clarification.

Ответить
Giorgio Guglielmone
Giorgio Guglielmone - 05.05.2020 23:34

You created a prefab named GameAssets with which you associated the GameAssets.cs script. Now the prefab where did you put it in the scene ?

Ответить
Walldoff Studios
Walldoff Studios - 29.04.2020 14:41

Is this like a more efficient way of object pooling or more like the same but just a lot cleaner? Or am i just completely wrong lol?

Ответить
Paolo Ferrer
Paolo Ferrer - 29.03.2020 23:12

Hey man I'm getting a stack overflow error when I try to use this GameAssets script in my game. Any ideas? Basically I set a reference to a projectile that my character is firing on the GameAssets script / prefab and then just instantiate it on the PlayerController script when I fire.

Ответить
BaconAndMinecraft
BaconAndMinecraft - 22.03.2020 15:00

Is this like a singleton?

Ответить
てくまやまなこん
てくまやまなこん - 11.02.2020 17:10

If you mentioned how to dispatch GameLogic.Init(), it makes this video better.

Ответить
Андрей Кадомский
Андрей Кадомский - 30.01.2020 01:32

I didn't get, the static method Init from gameassets, where it's called?

Ответить