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 /
avatar image
0
Question by $$anonymous$$ · Jun 06, 2019 at 12:38 AM · guimousepositiongui.box

UI Box to appear on mouse position

Im creating a UI box to appear as a "loading" bar before instantiating an object over it and destroying the GUI box. The way my game is set up now is the object that appears after the loading bar completes is instantiated on my mouse position in an OnMouse button down method. Everything works correctly except the UI Box is spawned in the upper left corner of my screen when I insatiate the game object. Ill post the code below to clarify.

My script attached to my object im instantiating:

 public class buildProgBar : MonoBehaviour
 {
     private buildItem[] gameObjects;
     float timeLeft;
     public float barDisplay;
     private Vector2 mousePos;
     private Vector2 size;
     public Texture2D emptyTex;
     public Texture2D fullTex;
     private SpriteRenderer sprite;
 
     private void Start()
     {
         gameObjects = GameObject.Find("BG").GetComponent<GameManager>().buildItems;
         timeLeft = gameObjects[GameManager.selectedStruct].BuildTime;
         sprite = gameObject.GetComponent<SpriteRenderer>();
         sprite.enabled = false;
         size = new Vector2(100, 15);
         mousePos = new Vector2(GameManager.spawnPosition.x, GameManager.spawnPosition.y);
 
     }
 
 
 
     void OnGUI()
     {
 
 
         //BG
         GUI.BeginGroup(new Rect(mousePos.x, mousePos.y, size.x, size.y));
         GUI.Box(new Rect(0, 0, size.x, size.y), fullTex);
 
         //Fill
         GUI.BeginGroup(new Rect(0, 0, size.x * barDisplay, size.y));
         GUI.Box(new Rect(0, 0, size.x, size.y), emptyTex);
         GUI.EndGroup();
         GUI.EndGroup();
     }
 
 
     private void Update()
     {
 
         if (timeLeft > 0)
         {
             timeLeft -= Time.deltaTime;
             barDisplay = timeLeft / gameObjects[GameManager.selectedStruct].BuildTime;
         }
         if (timeLeft <= 0)
         {
             sprite.enabled = true;
             enabled = false;
         }
     }


My script called by my Game Manager:

   private void OnMouseDown()
 {
     if (Input.GetMouseButtonDown(0) && !helpers.IsPointerOverUIObject())
     {

         spawnPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         spawnPosition.z = 0.0f;
         print(spawnPosition);
         selectedBI = buildItems[selectedStruct];
         toInstantiate = selectedBI.structure;
         if (buildStats.buildPoints >= selectedBI.cost && buildStats.maxPowerLevel + selectedBI.powerCost >= buildStats.maxPowerLevel)
         {
             buildStats.buildPoints -= selectedBI.cost;
             GameObject toBuild = Instantiate(toInstantiate, spawnPosition, Quaternion.identity);
             toBuild.transform.SetParent(mainCanvas.transform);
             buildStats.availablePower += selectedBI.powerCost;
             buildStats.maxPowerLevel += selectedBI.powerGain;
             
         }

     }
 }


Ive only been working in unity for around 3 days so pardon my mistakes. Thank you!

Comment
Add comment
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

2 Replies

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

Answer by niorg2606 · Jun 06, 2019 at 01:19 AM

Hey, welcome to Unity! You should use the new UI system instead of hard-coding it. Scroll through this doc in the manual. Essentially, you can make the UI objects actual GameObjects in your hierarchy.

Hope this helps!

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 $$anonymous$$ · Jun 06, 2019 at 01:34 AM 0
Share

Thank you for your answer! In the unity inspector after clicking the dropdown for game objects and selecting UI I don't see any object that would represent a loading bar. There are the usual slider, scroll view, etc. but nothing to represent a "loading" bar. Am I missing something?

avatar image
0

Answer by $$anonymous$$ · Jun 06, 2019 at 02:10 AM

I used a UI image to create a loading bar, made it a game object, instantiated, and then adjusted the fill amount. Thank you niorg2606!

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

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

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

Mouse moves over GUI 1 Answer

Can't display PNG texture within GUI calls 1 Answer

How to go to a new gui.window by clicking a gui.button? 1 Answer

strange Rect behavior on Y axis 1 Answer

Need help displaying score (JavaScript) 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