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

How to assign a Transform variable to a spawned prefab

Discussion in 'Getting Started' started by bah2, Jan 12, 2020.

  1. bah2

    bah2

    Joined:
    Jan 9, 2020
    Posts:
    5
    New to Unity, so not that sure about the terminology, but I'll explain:

    I'm doing a simple detect-and-chase scenario, while getting to know UI and the basics of Unity. The enemy is supposed to move around turning randomly at random intervals, until player comes within a certain radius from it. After it detects the player within this radius, it turns itself to face the player and starts to chase it. I have done this by using a public transform variable, which is used for getting the players position. I then assign the player object to by dragging it to it's field in the inspector. Then I cloned the enemy object to get more than one, and it works fine, so no worries there.

    But then I thought it would be nice if the enemies would spawn at certain intervals instead of just being the from the beginning, so I made the enemy a prefab, created an empty game object and wrote a script that spawns the prefab, and the actual spawning works just fine. What DOESN'T work, however, is the actual movement of the enemies. Now that the enemy object is a prefab, I can't assign the scene's player object to it's transform variable in the director, which makes sense. But at the moment it's the only way I know how to assign it.

    Wat do? How to I get my spawns of cubical nature to behave as if I had made one in the scene, and then just cloned it?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,851
  3. bah2

    bah2

    Joined:
    Jan 9, 2020
    Posts:
    5
    JoeStrout likes this.