• 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 /
  • Help Room /
avatar image
0
Question by gameshark123 · Jun 28, 2016 at 06:09 PM · scripting problemprefabs

How do I set a default target?

My "EnemyBasic" prefab is set to spawn in waves and successfully does but they need a target variable that is a transform type to have the "player" set, this way they move in a line towards the player. When they spawn, the player isn't set in the target variable because you can't save it in a prefab for some reason. Could I use a line of code in the wave spawn to set their target to the player? And if so, how? Thanks!

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 Mindmapfreak · Jun 28, 2016 at 07:10 PM 0
Share

You can most certainly do this via code, but to give a more specific answer you should post some of your code.

avatar image gameshark123 · Jun 29, 2016 at 10:40 PM 0
Share

var Distance; var Target : Transform; var lookAtDistance = 25.0; var chaseRange = 15.0; var attackRange = 1.5; var moveSpeed = 5.0; var Damping = 6.0;

var theDammage = 5;

var attackRepeatTime = 1; private var attackTime : float;

var Controller : CharacterController; var Gravity : float = 20; private var $$anonymous$$oveDurection : Vector3 = Vector3.zero;

function Start() { target = GameObject.Find("player"); attackTime = Time.time; Debug.Log("Start function has been run!"); }

function Update () {

 if (Respawn$$anonymous$$enu.playerIsDead == false)
     {
         Distance = Vector3.Distance(Target.position, transform.position);

         if (Distance < lookAtDistance)
         {
         lookAt();
         } 
         
         if (Distance > lookAtDistance)
         {
             renderer.material.color = Color.green;
         }
         
         if (Distance < attackRange)
         {
         attack();
         }
         else if(Distance < chaseRange)
         {
             chase();
         }
         

// if (Target != "player") // { // target = GameObject.Find("player"); // } } }

function lookAt() { renderer.material.color = Color.yellow; var rotation = Quaternion.LookRotation(Target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping); }

function chase() { renderer.material.color = Color.red;

 moveDirection = transform.forward;
 moveDirection *= moveSpeed;
 
 moveDirection.y -= Gravity * Time.deltaTime;
 Controller.$$anonymous$$ove(moveDirection * Time.deltaTime);

}

function attack() { if (Time.time > attackTime) { Target.Send$$anonymous$$essage("ApplyDammage", theDammage, Send$$anonymous$$essageOptions.DontRequireReceiver); Debug.Log("The enemy has attacked"); attackTime = Time.time + attackRepeatTime; } }

function ApplyDammage() { chaseRange += 30; moveSpeed +=2; lookAtDistance += 40; }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by PrisVas · Jun 28, 2016 at 07:48 PM

On the script that is attached to your "EnemyBasic" on the Start Function:

 void Start() {
         target = GameObject.Find("The Name Of Your Player");
     }
Comment
Add comment · Show 1 · 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 gameshark123 · Jun 29, 2016 at 10:39 PM 0
Share

That didn't work for me when I tried it. I put a Debug.Log after it and the line of code is being run, just not working right.

avatar image
0

Answer by UrielKane · Jun 30, 2016 at 05:19 AM

Like PrisVas Say you should use a GamObject.Find() or GamObject.FindWithTag(). But what you are miss here is that the target variable is a transform type not a gameobject type. So to solve this problem you ned to get the compent via GetComponent.<>(); comandline. Something like this.

 var target : Transform;
 
 function Start () {
     target = GameObject.Find("Player").GetComponent.<Transform>();
 }

This should work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

70 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 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 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 avatar image

Related Questions

Create a prefab of custom type 1 Answer

Connecting Prefabs together to make one Prefab via script? 0 Answers

How can I in-script create gameobject from prefab? 2 Answers

Problem renaming prefab instance in scene (from custom editor window) 1 Answer

Unity Addressable for player skins 0 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