• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by simonheartscake · Jan 31, 2012 at 01:54 PM · instantiateprefabaivariable

Any ideas on a simple AI script that doesn't use any drag/drop variables

So I have a spawn script that uses instantiate to spawn an enemy prefab. However the AI script on the enemy uses the position of the player within the hierarchy and instantiated prefabs do not keep these variables. does anybody have any advice on how to create an ai script that doesn't use this but still moves towards the player?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Jan 31, 2012 at 01:56 PM

If your player object has a tag, you can use

 GameObject.FindWithTag(tagName);

to get the first found object with that tag. If you are using the "Player" tag, and make sure that there is only ever one object with it, it should work fine.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image simonheartscake · Jan 31, 2012 at 05:00 PM 0
Share

Thanks alot, so you think like

var player : GameObject; var playerPosition : Vector3;

function Update (){

player = GameObject.FindWithTag(Player); playerPosition = player.transform.position;

?

avatar image simonheartscake · Jan 31, 2012 at 06:06 PM 0
Share

This worked great. Thanks

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Clone of prefabs copy original´s script state during runtime? 0 Answers

Passing a variable to the child of Instantiated object? 1 Answer

Updating a variable on a script in an instanced object 1 Answer

Change prefab before instantiation 1 Answer

Instantiate only one prefab 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges