Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

create a time bar for each object on screen

Discussion in 'Scripting' started by mirkojpn, Aug 5, 2018.

  1. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    hello everyone.
    i'm trying to find a way to create a timer bar for every object that i have on my scene, i'm just finding a way to create it on canvas with slider, but what i need it's a timer bar attached to the object that follow it.

    anyone know how to do it?
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Once you have got the time bar looking how you want it, create it as a prefab. Then, when you want it to follow a character, create a new instance from the prefab and make that new instance a child of the appropriate character.
     
  3. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class cube : MonoBehaviour {
    7.  
    8. public void SetGameManager( GameManager _inGameManager)
    9.     {
    10.         _GameManager = _inGameManager;
    11.     }
    12. private GameManager _GameManager;
    13.  
    14. [SerializeField]
    15. private GameObject slider;
    16. private const float _speed = 1.0f;
    17. private const float xmove = 1.0f;
    18.  
    19.  
    20.  
    21.     // Use this for initialization
    22.     void Awake () {
    23.         Instantiate(slider,gameObject.transform.position,Quaternion.identity,gameObject.transform);
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.         if(Input.GetKey(KeyCode.A))
    29.         {
    30.         _GameManager.dica++;
    31.         Debug.Log(_GameManager.dica);
    32.  
    33.         }
    34.  
    35.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    36.         {
    37.             Vector3 newPos = new Vector3(gameObject.transform.position.x-xmove*_speed + Time.deltaTime,0,0);
    38.             gameObject.transform.position = newPos;
    39.         }
    40.         if (Input.GetKeyDown(KeyCode.RightArrow))
    41.         {
    42.             Vector3 newPos = new Vector3(gameObject.transform.position.x+xmove*_speed + Time.deltaTime,0,0);
    43.             gameObject.transform.position = newPos;
    44.         }
    45.     }
    46.    
    47. }

    this is what i did, are going to instantiate it as child of cube, but doesn't follow it.
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Your code works for me. Do you have a script on the slider itself that sets its own position as well?
     
  5. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    not really, i suppose that i should set the canvas on the world space right?
    then set the position of the slider on it's script?
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Oh, I didn't realise you were using a canvas. I was just using 'standard' world space GameObjects. I created a cube, gave another cube as the slider prefab and it just worked. Why are you using a canvas?
     
  7. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126

    because it is the only way that i know to create a slider to use as power bar or something like that, actually i was able to set it as i want and now is following the cube movement, but if there is a better way you're welcome.
     
  8. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, glad you solved it. :)
     
    mirkojpn likes this.
  9. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    thank's you a lot for your help
     
    Doug_B likes this.