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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Control spawned object separately with the same Script.

Discussion in 'Scripting' started by krzysiu818, Dec 4, 2019.

  1. krzysiu818

    krzysiu818

    Joined:
    Dec 4, 2019
    Posts:
    10
    Hello,
    I am beginner at Unity programming. I have an problem with script that is resposible for moving object over game area, but these object are spawning every X secounds, so when I try to move one, every of them moves together.
    Can anyone help me to move them separately, not all together? (2D game)

    Thank you very much, Krzysztof
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Post the code you have so far (use [code ]code tags[/code] ), and we can help you figure out how to modify it to work correctly.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Show us your code because if you are maintaining a collection of all spawned objects and just targetting one of them from this collection and telling it to move, only that one should move.
     
  4. satchell

    satchell

    Joined:
    Jul 2, 2014
    Posts:
    107
    Is it like turn based movement?

    Use an if statement to check weather the object the script is attached to has the permission to move this turn.
     
  5. krzysiu818

    krzysiu818

    Joined:
    Dec 4, 2019
    Posts:
    10
    Code (CSharp):
    1. HIDDEN
    This is my code for this game
     
    Last edited: Dec 5, 2019
  6. satchell

    satchell

    Joined:
    Jul 2, 2014
    Posts:
    107
    I think you might be to broad in the touchCount any script asking in the update if the touch is greater than 0 will go... Maybe have a collider to accept an OnMouseDown() on the object to set it as active, script a bool set to true change
    if(Input.touchCount > 0)
    to
    if(bool == true)
    then when the target is reached clear the bool.
     
  7. krzysiu818

    krzysiu818

    Joined:
    Dec 4, 2019
    Posts:
    10
    I deleted the
    if(Input.touchCount > 0)
    section, and the result is exactly the same as previously.
     
  8. krzysiu818

    krzysiu818

    Joined:
    Dec 4, 2019
    Posts:
    10
    RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Vector2.zero);


    This code work for me, but just partly. I can move only fresh spawned objects, other object are immeditally moved to spawn point.