Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 NDJ23 · Dec 04, 2010 at 05:34 PM · prefabassign

Assign Transform from Hierarchy to Prefab script

Hey Everyone

I'm just getting started with Unity, right now I'm working on an iPhone project to learn the basics.

I am having some trouble with script variables though, and I'm sure it's just that I am learning as I stumble through each aspect. I have an enemy game object which has a script attached to it for the AI. This AI currently just tells it to chase a transform variable, which I then dragged my Player transform into. So everything seemed to be working great with the single enemy.

I then created an enemy prefab from the original, and setup a sphere collider to spawn a new prefab enemy when the player enters it. The problem is that the enemy prefab's AI script does not have the player transform assigned, so he just stands there after being created. I tried assigning the player transform as the default value, but this isn't allowed.

I know I must be missing something, and perhaps I'm going about this wrong. Any suggestions would be greatly appreciated.

Thanks!

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
1
Best Answer

Answer by Rennat · Dec 04, 2010 at 05:58 PM

After you instantiate a prefab you can save a reference to that instance, then get the enemy script component of the new instance (EnemyScript in the example below) and access any public variables or methods.

Javascript:

var newEnemy : GameObject; newEnemy = Instantiate(prefab, position, rotation);

var enemyScript : EnemyScript; enemyScript = newEnemy.GetComponent(EnemyScript);

enemyScript.targetTransform = transform;

Comment
Add comment · Show 3 · 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 NDJ23 · Dec 04, 2010 at 09:02 PM 0
Share

Thanks so much for the help, I will give this a try.

avatar image NDJ23 · Dec 04, 2010 at 10:10 PM 0
Share

I'm having a little trouble implementing this code, as I'm pretty novice with Javascript.

I think I understand everything except what to do with the last line. I believe that I should be assigning the transform of the player, but I'm not sure exactly how to do this.

For example the transform I want to assign the "target" variable to is named on the gameObject "Player". Can't figure out how to do that through code.

avatar image NDJ23 · Dec 04, 2010 at 10:45 PM 0
Share

Never$$anonymous$$d I think I figured it out, added another transform variable to the spawn object of type transform, and put my player there, then just assigned the enemy script target to it, and it works.

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Cannot drag and assign other class to prefab 1 Answer

Variable not Assigned 1 Answer

How to attach a script to a static Tree Prefab? 1 Answer

Network instantiate command do not execute in all client.. 1 Answer

How do I "Apply" a prefab if I have access to one of its instances via code? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges