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

SOLVED: Rotate to another instance of a target?

Discussion in 'Scripting' started by mhedges, Jan 30, 2020.

  1. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    Hello -

    I'm doing a game in which you must collect coins before the computer opponents do. You drive a car, the computer drives the opponents.

    The problem I have is as follows. The opponent car will rotate to and drive towards the coin, its target. It will work fine on the first coin, a prefab instantiated at a random location. Once that coin is collected, another coin prefab is instantiated. However, the opponent car seems to not recognize the new coin prefab target.

    What am I doing wrong? There is the prefab coin, and the first target coin is "recognized" by the opponent car.


    Thanks, regards.
     
  2. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    Solution:

    I inserted the following line in the opponent car's control script:

    Code (CSharp):
    1. target = GameObject.FindWithTag("Coin").transform;
    That way the newly minted (haha, instantiated) coin is found as it has the "Coin" tag.

    Thanks for reading!