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
1
Question by Alp-Giray-Savrum · Jan 29, 2014 at 11:51 AM · gameobjecttransformaifollowartificial intelligence

How to assign a transform object to Prefab ?

Hello Developers, I've got a problem right here. I've got 2 spaceships and their AI script. I can't assign my game object to theese scripts. I think that's because of i used "Transform" method. But When i change it to "GameObject.FindWithTag("Player");" transform method and follow method colappses. So what should i do ?

 enter code herevar moveSpeed : float;
 var player : Transform;
 var distanceToPlayer : float;
 
 function Update() 
 {
 
 transform.LookAt(player);
 
 if ((Vector3.Distance(transform.position, player.position) > distanceToPlayer))
 {
         transform.position += transform.forward * moveSpeed * Time.deltaTime;
 }
 
 else
 
 {
 
 transform.rotation.y = 180;
 transform.position += transform.forward * moveSpeed * Time.deltaTime;
 
 }
 
 }

my code is this.

Age 14, Turkey.

Comment
Add comment · Show 1
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 Boreale · Jan 29, 2014 at 12:09 PM 0
Share

$$anonymous$$aybe you should use GameObject.FindWithTag("Player").transform to get player's Transform component for your AI script?

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by BoydHamilton · Sep 19, 2020 at 07:06 PM

             player = GameObject.FindWithTag("Player");
             target = player.transform; 
 

get the gameobject, then get the gameobjects transform. this worked for me, hope it works for you as well!

Comment
Add comment · 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
1

Answer by CHPedersen · Jan 29, 2014 at 12:04 PM

It doesn't have anything to do with your use of a public Transform variable to hold a reference to the player. Instead, it is probably because Unity doesn't serialize scripts (and public variables) until all scripts pass compilation. Serialization is the feature that makes stuff appear in the Inspector. I don't see anything wrong with your script as posted above, but perhaps there are still compiler errors elsewhere in your code?

Clear all compiler errors, and then try again. "Player" should appear in the Inspector in the editor for this script, and then you can drag the gameobject onto it.

Comment
Add comment · Show 9 · 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 Alp-Giray-Savrum · Jan 29, 2014 at 12:16 PM 0
Share

alt text

this is what i mean; I can't able to attach my player when i try to attach, that kinda alt text

screen appears and nothing else.

ekran alıntısı.jpg (225.9 kB)
ekran alıntıssı.jpg (19.8 kB)
avatar image CHPedersen · Jan 29, 2014 at 12:20 PM 0
Share

Ah, okay, that's because you're clicking the little dotted circle, which makes the editor look for assets to attach. But a "Transform" is not a file, and so does not appear as an Asset file in this list.

To attach it, drag and drop the Player gameobject with your mouse from the Hierarchy to the script ins$$anonymous$$d.

avatar image Alp-Giray-Savrum · Jan 29, 2014 at 12:28 PM 0
Share

But i need it as Transform, Not GameObject, When i turn it to GameObject; I can't able to use transform operations (Like, position, rotation etc.) and it's not allowed, I can't attach :S

avatar image CHPedersen · Jan 29, 2014 at 12:30 PM 0
Share

You don't have to change it to GameObject. Dragging a GameObject from Hierarchy onto a variable of type Transform will set that variable to Transform too. Just try it.

avatar image CHPedersen · Jan 29, 2014 at 01:14 PM 2
Share

Oooh, right, then I get it. Sorry about missing that (it's even in the title!). ;)

This is because a prefab is not a part of any particular scene, so you can't attach gameobjects that belong to a specific scene to it. Imagine what would happen if you did, and then in another scene where the player gameobject might not exist, you try to instantiate a copy of the prefab. What Transform would it store in the variable? It can't know, because the gameobject doesn't exist anymore.

To fix this, you need to grab a reference to the player gameobject through code, I'm afraid. So you were right to begin with - you have to move to one of the GameObject.Find methods.

Try to add this, for example:

 function Start () 
 {
     player = GameObject.Find("Player").transform;
 }
Show more comments
avatar image
0

Answer by thekoening · Jan 05, 2015 at 02:10 PM

Make your player a prefab.

Comment
Add comment · 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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find transform.position of Another Game Object 1 Answer

Problems with simple AI script 3 Answers

Enemy Follow Player 1 Answer

Problem with trigger 1 Answer

Unity Best Practices for Physics + AI 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