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 /
  • Help Room /
avatar image
0
Question by oxysplatter · Jul 27, 2019 at 10:00 PM · 3d

Hopefully simple question about data from scriptableobject.

I have a simple space game set up and I have the UI displaying data about planets on mouseover.

Each planet has a set of strings that contain data like planet name etc. that is referenced from a Scriptable object.

Here is the code for that:

 [CreateAssetMenu(fileName = "New PlanetData", menuName = "PlanetData")]
 public class PlanetData : ScriptableObject
 {
 public string planetName;
 public string planetDescription;
 public string planetType;
 public string planetMass;
 public string planetMinerals;
 }
 

So I have created a bunch of assets that get data from this SO that is then attached to each planet GameObject in editor, and on mouseover, you get a pop up with this data, which is sent to a canvas object, that contains the text fields that Im using to display this data.

I link the text element from the inspector to the game object.

This is adapted from a brackeys tutorial.

Here is the code for the data:

 public class PlanetUI : MonoBehaviour
 {
 public PlanetData planet;
 public Text name;
 public Text description;
 public Text type;
 public Text mass;
 public Text minerals;
 public void Update()
 {
 name.text = planet.planetName;
 description.text = planet.planetDescription;
 type.text = planet.planetType;
 mass.text = planet.planetMass;
 minerals.text = planet.planetMinerals;
 }
 }


The issue I have is that the text does not update with the new data for each planet. I have had it work before when using a seperate canvas for each game object individually but this seems convoluted and probably unnecessary

So no matter which planet i mouse over, the UI displays the same strings.

Is it possible to refresh or update the strings?

Im sure its a simple fix but I just cant get my head around it.

Any help would be appreciated.

edit(moved from answer)

 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlanetInfoUI : MonoBehaviour
 {
     private RaycastHit hit;
     public Ray ray;
     [SerializeField]private GameObject planetInfo;
 
     void Start()
     {
         planetInfo.SetActive(false);
     }
 
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(Physics.Raycast(ray, out hit, 3.1f))
         {
             if(hit.collider.tag == "RayPlane")
                 {
                     planetInfo.SetActive(true);
                     planetInfo.transform.position = Camera.main.WorldToScreenPoint(hit.collider.transform.position);
                 }
         }
         else
         {
             planetInfo.SetActive(false);
         }
     }
 }

https://media.giphy.com/media/QxdehewWvajPf7DP9c/giphy.gif

Comment
Add comment · Show 2
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 SirPaddow · Jul 27, 2019 at 10:18 PM 0
Share

I don't see a problem with your SO or with your UI update function. The only reason I can think of is that you don't properly assign the PlanetData in your UI on your mouseover. Could show us this part too?

avatar image oxysplatter SirPaddow · Jul 27, 2019 at 10:30 PM 0
Share

Ill deal with it now. Thanks.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SirPaddow · Jul 28, 2019 at 09:50 AM

The problem is that you never assign the data specific to the hovered planet. Your PlanetData SOs contain the data for one planet, and you need to link it to the corresponding gameobject in some way. Then, in PlanetInfoUi, you need to get this data from the hit object, and link it to your PlanetUI. So, something like this:

(Assuming you have a "Planet" MonoBehaviour on your planets):

 public class Planet : MonoBehaviour
 {
     public PlanetData data;
 }

On each of your planet gameobjects, assign the corrsponding SO.

As for the PlanetUI, you could directly assign the data and wait for the update, but you should always try to avoid using the Update function where not necessary. Something like this:

 public class PlanetUI : MonoBehaviour
 {
     public Text planetName;
     public Text description;
     public Text type;
     public Text mass;
     public Text minerals;
 
     public void SetPlanetData(PlanetData planetData)
     {
         planetName.text = planetData.planetName;
         description.text = planetData.planetDescription;
         type.text = planetData.planetType;
         mass.text = planetData.planetMass;
         minerals.text = planetData.planetMinerals;
     }
 }
 

Finally, in the PlanetInfoUI, replace the planetInfo by the component itself rather than the gameobject, because you will need to have that ui component specifically. Then, in your update function, get the Planet component of the hovered gameObject and assign the data to the ui component:

 public class PlanetInfoUI : MonoBehaviour
 {
     private RaycastHit hit;
     private Ray ray;
     [SerializeField] private PlanetUI planetInfo;
 
     void Start()
     {
         planetInfo.gameObject.SetActive(false);
     }
 
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         bool showPlanetInfo = false;
         if (Physics.Raycast(ray, out hit, 3.1f))
         {
             Planet hitPlanet = hit.collider.GetComponent<Planet>();
             if (hitPlanet)
             {
                 showPlanetInfo = true;
                 planetInfo.SetPlanetData(hitPlanet.data);
                 planetInfo.transform.position = Camera.main.WorldToScreenPoint(hit.collider.transform.position);
             }
         }
         planetInfo.gameObject.SetActive(showPlanetInfo);
     }
 }

I'm not sure what is the RayPlane tag in your script, but each planet should have its own collider so you can know which is which.

Hope it helps, good luck :)

Comment
Add comment · Show 1 · 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 oxysplatter · Jul 28, 2019 at 09:55 PM 0
Share

This has been a headache for me for days. I had realised early on that there was some way of referencing the variables, missing from my script. But just didn't know where.

For the time being Ive linked each variable individually rather than getting the data from the SO instance asset, but looking over what you've given me here, I think I need to go back and re write my code.

Thanks so much for your help.

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

136 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 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

Monitor Screen in 3D game (UI representation) 0 Answers

Jump Script not Working JavaScript 1 Answer

Need help designing a sinking platform that lets objects pass through from underneath 1 Answer

How to have a 2D level minigame in a 3D world. 1 Answer

How to move whilst airborne (using standard third person character script)? 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