Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

gameObject.Find, .transform.position, variables, for loops -HELP MEEE-

Discussion in 'Scripting' started by xxlilxjxx, Dec 21, 2014.

  1. xxlilxjxx

    xxlilxjxx

    Joined:
    Jul 21, 2014
    Posts:
    28
    _________________________Script____________________________

    function bonus(){
    var BonusXAxis = Random.Range(-2.7,1.78); //Random point across screen
    for (var BonusYAxis = 0; BonusYAxis >= 3.2; BonusYAxis++){ //Sliding up screen
    yield WaitForSeconds(1); //Time delay for each reposition
    gameObject.Find("Bonus").transform.position = Vector3(BonusXAxis, BonusYAxis, -1.579669); /Change pos
    }

    _________________________Script____________________________


    (There is a execution for this in the update function)
    I want to add a bonus money amount. An indication of the bonus should slide up across the screen. And from a random point across the screen.

    Sadly for me :c

    • The position will not change at all
    And the console shows no error. What did I do wrong?
     
  2. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    What is the game object "Bonus"? Is it a 2D GUI element? Is it a 3D TextMesh?

    From your description it seems like an on-screen GUI element. Make sure it's transform.position isn't in "screen space", meaning that the position of the object is normalized (the X/Y/Z positions on the screen only go from 0-1, with 0 being one side of the screen and 1 being the opposite).

    I'll give my usual suggestion for these circumstances (I should really just make this text my signature :p):

    When you're writing code, and you're not sure how any part of it works, build a simple test first until you understand it.

    In your case, remove the line with the Random.Range call, the yield WaitForSeconds, really everything except for the gameObject.Find("Bonus").transform.position call (or whatever part of your code is not working at that time). Then play with the position manually until you understand how to position/move it on the screen as you would like. Once you understand how this Bonus object's position works, then you can begin to add the rest of the code around it (random position, the yield call, etc.).

    Good luck!