Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 31, 2018 at 03:27 AM by RX187 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RX187 · Aug 27, 2018 at 09:19 AM · scriptableobjectinventoryinventory systemscriptable objecthow to

Scriptableobject List and Instantiating objects from it

Hello, I am making a simple scriptableobject inventory to store the objects of a in-game store. All was good and wall until it came to the part where I need to instantiate all of the objects from that list and it only instantiates the last one from the list no matter what I tell it to do.

For example I try doing this:

     public List<GameObject> gameStoreInventory = new List<GameObject>();
     public GameDatabase gameDatabase;
     public GameObject slotHolder;
 
     private void Start()
     {
         PopulateGames();
     }
 
     public void PopulateGames()
     {
         GameObject obj = Instantiate(gameDatabase.games[0].GamePrefab) as GameObject;
         gameStoreInventory.Add(obj);
         obj.transform.SetParent(slotHolder.transform, false);
     }
 }
 

And I'm trying to instantiate the first item in the list but for some reason the last time in the list would always instantiate instead.

The scriptableobject script is this one:

 [CreateAssetMenu(menuName = "New Game Database")]
 public class GameDatabase : ScriptableObject
 {
     public List<Games> games = new List<Games>();
     
     public enum GameType { Action, Adventure, RPG, Puzzle, FPS, RTS };
     public enum GameQuality { Bad, Decent, Good };
     
     [System.Serializable]
     public class Games
     {
         public GameObject GamePrefab;
         public string Name;
         public string Description;
         public GameQuality GameQuality;
         public GameType GameType;
         public string Review;
         public int Cost;
     }
 }

And every item that is set in that list has this script attached to it:

 public class Game : MonoBehaviour
 {
     public Text Name;
     public Text Description;
     public Text Quality;
     public Text Genre;
     public Text Review;
     public Text Cost;
     public GameObject GamePrefab;
     
     
     public GameObject tooltip;
     public GamesManager gamesManager;
 
     private void Start()
     {
         gamesManager = GameObject.Find("Game Store Menu").GetComponent<GamesManager>();
     }
     
     private void Update()
     {
         for (int i = 0; i < gamesManager.gameDatabase.games.Count; i++)
         {
             Name.text = gamesManager.gameDatabase.games[i].Name;
             Description.text = gamesManager.gameDatabase.games[i].Description;
             Quality.text = gamesManager.gameDatabase.games[i].GameQuality.ToString();
             Genre.text = gamesManager.gameDatabase.games[i].GameType.ToString();
             Review.text = gamesManager.gameDatabase.games[i].Review;
             Cost.text = gamesManager.gameDatabase.games[i].Cost.ToString();
             GamePrefab = gamesManager.gameDatabase.games[i].GamePrefab;
         }
     }
 

This is how the scriptableobject list looks like in the inspector:

alt text

And this is how it look's like I explained above where no matter what I do the last item is always being displayed:

alt text


Does anyone have any tip's on how to fix this? What I doing something wrong? Thanks in advance!

example.png (20.6 kB)
example2.png (32.3 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sonky108 · Aug 27, 2018 at 09:28 AM 0
Share

Every GamePrefab object instantiated in the scene has Game $$anonymous$$onoBehaviour attached, right? Why are you loooping through all games in gameDatabase and set all the credentials every tick? It seems to me that you set all of these texts every frame so actually you see the object as the last one. Every frame you change all of texts components, so you should see rapid flickering or random still text.

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by FernandoHC · Aug 27, 2018 at 10:16 AM

First, Whenever having this kind of error, where you are getting one thing instead of the other, always make the differences between the objects to have as discrepancy as possible. In your cases I see the objects are exactly the same, save the name that changes only one character. That in itself is a cause for confusion.
Secon, why are you setting the values on an Update, and with a for? Not sure where your Game class is assigned to, but what is happening there is that you're going through your whole "gameDatabase" on each update frame, and assigning all values to the Game class public variables, only the last values will be displayed, because it as the last one put in. So if you want to make this work you have to assign the values only at the time the object is instantiated and never again.
On a side note, do not recommend calling a class "Game" unless it really is a singleton game manager class.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by misher · Aug 27, 2018 at 10:47 AM

You should work on clear separation of DATA, UI and other Controllers Scripts.
You can define all data in several ScriptableObjects. Having a prefab as a data is really a bad practice, this is the task of UI part, your evety UI script should contain references to own ui template (it can be also a disabled gameobject somewhere in your canvas somewhere not strictly a prefab) and then instantiate this template with using provided data. I would recommend you also to pass events through Scriptable Objects as well as it can make your architecture loosely coupled, here you can find a simple and powerful architecture pattern:

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by RX187 · Aug 27, 2018 at 07:37 PM

@Sonky108 @FernandoHC @misher Thank you all for you answers and tips I will take those into account. So far I have managed to solve the problem with you said, I can't believe I didn't realize the mistake with the for loop thing in the update sooner.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Instantiate objects that are Scriptable Objects 0 Answers

Need some advice for my inventory system 0 Answers

Inventory with Upgradable Items and Varying Stats 2 Answers

Inventory armor wielding proplem,How to convert from derived to base 1 Answer

Need opinions on handling Scriptable Objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges