Unity C# Programming: Storing Prefab GameObjects in a List

Поделиться
HTML-код
  • Опубликовано: 11 сен 2024
  • A quick demo on how to easily access Prefab GameObjects in Unity by loading them into a List

Комментарии • 36

  • @hplovecraftmacncheese
    @hplovecraftmacncheese 2 года назад +3

    I needed something very simple to create a working code example of my first List. This did the trick.

  • @XxJaimeSGxX
    @XxJaimeSGxX 3 года назад +6

    You really should create a complete Unity C# course, even if you did so in Udemy for a cost: I'd totally pay for it because you make complex things into simple ones. Looking forward for your next tutorials!

  • @alanomari4290
    @alanomari4290 3 года назад +2

    This helped me get the ball rolling on a spell system for my Player, thank you.

  • @hossamkh4380
    @hossamkh4380 Год назад

    i have to confess that you r th best who expaned this well even unity devs dont know how to explain like you

  • @marc-antoinegiguere6323
    @marc-antoinegiguere6323 2 года назад +1

    Great video, I love the relax and informative tone and the examples are spot on!!

  • @mattisriiskjrbentsen3604
    @mattisriiskjrbentsen3604 Год назад

    Already liking this after 2:22 in and I LOVE you for giving me the answer, just like that! Looking forward to seeing the rest of the video, great explainer! Thank you kindly!
    Also I LOVE your accent :D

    • @Digestible
      @Digestible  Год назад

      Thanks for the nice comment! I appreciate it!

  • @andrewmettler2228
    @andrewmettler2228 2 года назад

    Ma'am, you are a life saver. I have been struggling to figure this out for months. Thank you so much!

    • @Digestible
      @Digestible  2 года назад +1

      Thanks for such great feedback! I'm glad that it helped you!

    • @andrewmettler2228
      @andrewmettler2228 2 года назад

      @@Digestible you're welcome! I do have a question though if you don't mind. I'm using this method to instantiate a hand of cards from a deck. How would I go about instantiating the prefabs on a specific game object (an empty game object called "hand"), and making them the child of that object?

    • @Digestible
      @Digestible  2 года назад +1

      @@andrewmettler2228 Luckily it looks to be not a big deal to do this (just tried it out and it seemed to work fine)
      1) create the empty game object in the hierarchy
      2) in the script define the parent object (I put this line right below the line where i set up the list - in the class but not in a method:
      public Transform parent; //in this example i'm just calling it "parent"
      3) add an additional parameter to the instantiate command to include the parent object. If I used the example from this video it would look like this:
      Instantiate(targetList[i], new Vector3(randomx, 0, randomz), Quaternion.identity, parent);
      4) save your script & go back into unity
      5) click on the object that holds your script in the hierarchy - you should now see a spot in the inspector called "parent". Drag your empty gameobject to it.
      6) Run it and now the prefabs will be instantiated into the parent.
      You may have to change how you have the positions set up for your own example (you likely dont want a random x and z value,...) but this should add them as children. also, here is the Unity documentation that shows all the variations you can use with Instantiate - you may not need the same rotation/position settings and such so you might be able to pick a different one that includes the parent docs.unity3d.com/ScriptReference/Object.Instantiate.html

  • @seb.5053
    @seb.5053 3 года назад +1

    Thank you! This is the video I've been looking for! and as all of your previous ones the quality is absolutely great!

    • @Digestible
      @Digestible  3 года назад

      Thanks! I'm glad it helped!

  • @briana3186
    @briana3186 Год назад

    This is such a good channel! Thank you! Hope you keep going, you deserve way more subscribers.

  • @exe.m1dn1ght
    @exe.m1dn1ght 2 года назад +1

    would love to see more videos from you .. :D

  • @BrendenWatsonDrumMajor
    @BrendenWatsonDrumMajor 2 года назад

    You have just saved the Dwarves of Norr, from my game, thank you!

    • @Digestible
      @Digestible  2 года назад

      Glad to hear it! Always happy to save dwarves!

  • @James08244
    @James08244 2 года назад

    exactly what I wanted to know, thank you so much.

    • @Digestible
      @Digestible  2 года назад

      Awesome - glad it helped!

  • @atokosto
    @atokosto 2 года назад

    Helpful Tutorial,
    Thanks

  • @unitylearning8736
    @unitylearning8736 Год назад

    THANK YOU SO MUCH

  • @achyuth_kammari
    @achyuth_kammari 2 года назад

    Good afternoon....I have one question.....if I would like to instantiate a prefab with drop-down....when I instantiate the one of the prefab, I would like to destroy the rest of the instantiate prefab which are already instantiate......

  • @kcatholywater7134
    @kcatholywater7134 Год назад

    Sorry to interrupt you from all those compliments, but somehow I typed “
    TextAsset text = (TextAsset) Resource.Load(file name)
    Debug.Log(text);” and the output is null.
    Which means the program deemed the file/folder were not existed
    Which it existed in the Resources.
    Can you explain how to deal with it?

    • @Digestible
      @Digestible  Год назад

      note: I havent personally used code specific to what you posted so at this point I'm just taking a couple of guesses.
      It's hard to tell without seeing your complete script BUT I think one thing to start with is that you have a typo - you are missing the "s" in Resources.Load (you have Resource.Load). (i'm also assuming that "file name" is a placeholder for your actual filename just for this post? otherwise that would cause an issue as well )
      The other thing I wonder if trying to debug.log the TextAsset directly may not work - you may have to use .text to access it (in this case text.text) And, It may not be a problem but naming a variable "text" may also cause confusion with the code since "text" is used as a property as well (might be better to name it something not a keyword)
      check out this stackoverflow entry - the top answer has code similar to what you are trying stackoverflow.com/questions/21583104/unity-load-text-from-resources

  • @3DComparison
    @3DComparison 3 года назад

    Thank you! I have learned so much from this video! I am curios if it is possible to define a script that will loop between game objects within the list. Only having once active at a time.

    • @Digestible
      @Digestible  3 года назад +1

      You totally could do that. Instead of using a random number as the index of the list as i did in this example you would then use a variable to keep track of the item it is on and then increment (or decrement) the index to loop through. I have a video on how to do this with arrays - it would work the same for lists. Here is the video: ruclips.net/video/MkjVU5F1it0/видео.html

  • @Mr.Yolo.Greedy
    @Mr.Yolo.Greedy 3 года назад

    Thank you!

  • @ryslanxd8787
    @ryslanxd8787 2 года назад

    thanx)

  • @bryancarrasquillo66
    @bryancarrasquillo66 2 года назад

    Great video! I dont know why on my part is only creating me a DontDestroyOnLoad with a childObject called [Debug Updater].

  • @nomad2809
    @nomad2809 Год назад

    This video is obsolete