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 eeveelution8 · Apr 12, 2014 at 08:18 PM · getcomponentfunction updateassign-variabletransform.

reference an object without pre-assigning.

Im at a stump because I have had a problem with functions like GetComponent();.

this is a simple example script for reference:

 var myTransform : Transform;
  
 function Update () {
  
  transform.LookAt(myTransform);
  
 }

The script works fine for making my enemies face the player, and all is good. I place the enemy in the scene, attach the script, and set the transform to my player and play it.

What the problem is however, Is I cannot have additional enemies spawn into the scene because The transform will be blank, so the enemy will just shoot at a wall. And having to have every enemy and object on the map have to be pre assigned is tiresome, time consuming and limits thegames capabilities.

What I am wondering, is how to make the first line of the script, or any function looking like this:

 var HPtext : GUIText = gameObject.GetComponent(GUIText);

be able to reference an object and target it without having to be pre assigned. How would I go about doing this?

Comment
Add comment · Show 2
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 getyour411 · Apr 13, 2014 at 01:23 AM 0
Share

The example you've given is typically not used that way. This example is the classic 'cache of transform' and it is used for the actual gameobject the script is attached to (ie. the "my" in myTransform), not some other transform like the player. While it's not technically wrong, I point it out so you'll better understand other examples that cache the transform.

http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/

https://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent

avatar image eeveelution8 · Apr 13, 2014 at 01:28 AM 0
Share

I learned that the hard way i guess, I just returned to this page because I fixed the problem, but I'll bookmark those pages thanks. :3

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by eeveelution8 · Apr 13, 2014 at 01:29 AM

Thanks for the help anyone who responded to this, I fixed the problem.

 var Target : Transform;
 
 function FindTarget () : Transform {
     if(GameObject.FindWithTag("Player")){
         who = GameObject.FindWithTag("Player"); //We have found a Target
     }
     return who.transform;
 }
 
 function Update () {
 if(!Target){
         Target = FindTarget(); return;
     }
 transform.LookAt(Target);
 
 }
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 nicolasjr · Apr 12, 2014 at 09:03 PM

To find your player with any enemy script, you can simply create a tag and set it on your player, in the inspector.

then, to find your player:

 Transform player = GameObject.FindGameObjectWithTag("Player");

Does that solves your problem, Eevee?

Comment
Add comment · Show 4 · 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 nicolasjr · Apr 12, 2014 at 09:04 PM 0
Share

Sorry, the code is wrong, it should be:

 Transform player = GameObject.FindGameObjectWithTag("Player").transform;
avatar image eeveelution8 · Apr 13, 2014 at 12:13 AM 0
Share

It doesnt work :/

It has a problem with the beginning part being Transform player

avatar image ForeignGod · Apr 13, 2014 at 12:38 AM 0
Share

shouldnt it be

myTransform = GameObject.FindGameObjectWithTag("Player").transform;

?

avatar image eeveelution8 · Apr 13, 2014 at 12:44 AM 0
Share

still doesn't work.

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

Getting script from another script bug 1 Answer

How to access scripts other than by name. 1 Answer

Issues using GetComponent 1 Answer

Changing variables in another script 3 Answers

how to make one script change a variable in another scipt 2 Answers


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