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

Make a game object move through tagging?

Discussion in 'Scripting' started by MusicBoxx, Jul 24, 2018.

  1. MusicBoxx

    MusicBoxx

    Joined:
    Jul 17, 2018
    Posts:
    23
    Hello, I wanted to find out whether or not it is possible to make a game object move through tagging?
    Code (CSharp):
    1. if (_matches == 0)
    2.         {
    3.             Player = GameObject.FindWithTag("Player");
    4.             transform.position = Vector3.MoveTowards(transform.position, targetPos, 0.5f);
    5.  
    6.         }
    Something like that?

    I want my player to move automatically to a certain position if the goal is finished and since references from other scripts won't work (It won't give me any warning or errors, so I have no idea what might be wrong) I decided to move my Player through game manager, however I don't seem to find a way to do it.

    Huge thanks for those that are trying to help me out
     
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Hi,
    if this script is attached to the player so you dont need to acceed to it by a tag. Otherwhise ok BUT change
    transform.position into Player.transform.position
     
  3. MusicBoxx

    MusicBoxx

    Joined:
    Jul 17, 2018
    Posts:
    23
    Nope. GameManager isn't attached to the Player and it doesn't find the PlayerScript for whatever reason. I read unity docs and there was no help whatsoever. So that's why I'm trying to make it move the other way.:)
     
  4. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Player script? I dont see any script instance or reference in the lines you posted. You just acceed to the GameObject Player.
     
  5. MusicBoxx

    MusicBoxx

    Joined:
    Jul 17, 2018
    Posts:
    23
  6. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Code (CSharp):
    1. PlayerScript script;
    2. GameObject Player;
    3. void Start()
    4. {
    5. Player = GameObject.FindWithTag("Player");
    6. script = Player.GetComponent<PlayerScript>();
    7. }
    8. Void Update() // for example
    9. {
    10. Player.transform.position = vector3.MoveTowards(Player.transform.position, targetPos, 0.5f);
    11. }
     
    MusicBoxx likes this.
  7. MusicBoxx

    MusicBoxx

    Joined:
    Jul 17, 2018
    Posts:
    23
    Thank you. Now it works.
     
  8. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Yw ;)
    enjoy.
     
    MusicBoxx likes this.