Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
-1
Question by putlucky · May 25, 2015 at 01:25 PM · c#nullreferenceexceptionscriptableobjectreference

Getting a reference to a group of ScriptableObjects implemented at runtime?

So I think that title's appropriate, but here's what's going on:

I have about 30 scriptable objects, of type GameItem:

 public class GameItem : ScriptableObject {
 
     public int itemID;
     public string itemName;
     public string merchantName;
     public string itemDesc;
     public string cost;
     public Sprite merchantIcon;
     
 }

And what I can currently do with this is select 8 ScriptableObjects at random (eight items), from a list of all of them, and then put those 8 in a scroll-down display panel.

What I want to do next is to open up a new window whenever I select a button, displaying whatever item advertised by said button.

So here's the selection process for my eight buttons:

 foreach (var item in itemsList)
         {
             if(randomValues.Contains(item.itemID))
             {
                 GenerateInfo(item);

(randomValues is an array which stores eight random numbers within the constraints of itemList.Count).

This line:

 GenerateInfo(item);

Is what I want to talk about, because I'm passing the item through into another method 'GenerateInfo', which I want to deal with opening up the other window to display further information on the item.

I have an:

  public Button.ButtonClickedEvent openInfo;

Which calls GenerateInfo.

alt text

In the above image you can see that currently there isn't a reference to a variable of type GameItem.

In this case, it is referring to:

 public void GenerateInfo(GameItem anItem)

anItem, which I use to set the FurtherInformation panel's various properties to equal the properties of that variable. Say I want to display the title of whatever item is pressed, my logic is to take the current item and then access it's 'name' property.

THE PROBLEM

  • I don't know what to put down as a reference for this variable of type GameItem. I have the option to put down one of my scriptable objects, but that would defeat the point of selecting eight at random from my list. Since what object is used is subject to the if statement, comparing the item's ID to a random number generated.*

Let me know if there's anything I need to clear up, I have considered making the variable private.

poplist.png (8.8 kB)
Comment
Add comment · Show 3
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 siaran · May 25, 2015 at 02:41 PM 0
Share

It's not really clear to me what the problem is...

You have a reference to a GameItem to do something with. So what GameItem do you want to use there? Then put that one in the reference... That's probably not much help but I don't really understand your question.

avatar image putlucky · May 25, 2015 at 03:24 PM 0
Share

@siaran I don't know what to put into the slot, since the GameItem used is subject to random selection. To explain, I loop through a list of all scriptable objects of type GameItem, I select eight at RANDO$$anonymous$$ using an array. For each item selected as I loop through I send that item to the method GenerateInfo, which takes it as a parameter. In GenerateInfo I use that GameItem to populate the information (UI Text, Images etc.) on a seperate panel. That's the exercise. But you see, through all that I never actually $$anonymous$$NOW definitively which GameItem will be selected so I CANT put down a reference in the editor, because the process is random.

avatar image siaran · May 25, 2015 at 05:58 PM 0
Share

So you can just assign it in your script after you've made a selection then, right?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Trouble creating reference of image in script. 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Null Reference Exception For Blocks C# 1 Answer

How do you save a reference to a prefab's script in a ScriptableObject? 0 Answers


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