Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Jay9999 · Jul 31, 2015 at 09:41 AM · playersceneenemytarget

enemy targeting player in different scene

Hi.How do i make enemy from another scene to target the player when he goes on other scene ? (The enemies on level 2 Will target the player when the player Will be on level 2 )... I tried this but I've got a null reference exception

 var Distance;
 var Target : GameObject;
 var lookAtDistance = 25.0;
 var chaseRange = 15.0;
 var attackRange = 1.5;
 var moveSpeed = 5.0;
 var Damping = 6.0;
 var attackRepeatTime = 1;
 
 var TheDammage = 40;
 
 private var attackTime : float;
 
 var controller : CharacterController;
 var gravity : float = 20.0;
 private var MoveDirection : Vector3 = Vector3.zero;
 
 function Start ()
 {
     attackTime = Time.time;
     target = GameObject.Find("Health");
 }
 
 function Update ()
 {
     Distance = Vector3.Distance(Target.position, transform.position);
     
     if (Distance < lookAtDistance)
     {
         lookAt();
     }
     
     if (Distance > lookAtDistance)
     {
         GetComponent.<Renderer>().material.color = Color.green;
     }
     
     if (Distance < attackRange)
     {
         attack();
     }
     else if (Distance < chaseRange)
     {
         chase ();
     }
 }
 
 function lookAt ()
 {
     GetComponent.<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 ()
 {
     GetComponent.<Animation>().Play("run");
     GetComponent.<Animation>()["run"].speed = 1;
     GetComponent.<Renderer>().material.color = Color.red;
     
     moveDirection = transform.forward;
     moveDirection *= moveSpeed;
     
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }
 
 function attack ()
 {
     if (Time.time > attackTime)
     {
         GetComponent.<Animation>().Play("attack");
         GetComponent.<Animation>()["attack"].speed = 1; 
         
         Target.SendMessage("ApplyDammage", TheDammage);
     //    Debug.Log("The Enemy Has Attacked");
         attackTime = Time.time + attackRepeatTime;
     }
 }
 
 function ApplyDammage ()
 {
     chaseRange += 30;
     moveSpeed += 2;
     lookAtDistance += 40;
 }

Can someone help me please Thank you !

Comment
Add comment · Show 8
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 tanoshimi · Jul 31, 2015 at 09:54 AM 0
Share

@Hellium's absolutely right - this makes no sense. How can an enemy on level 2 target the player who's on level 1?

avatar image Jay9999 · Jul 31, 2015 at 09:57 AM 0
Share

I dont want to run two scenes at the same time . There is the first scene and if the player enter in a specific area ,scene 2 is load . When scene 2 is load , the enemies in scene 2 cant find the target "Health" because we cant have an other first person controller in the second scene . it´s why I'm trying to change this script .

avatar image Jay9999 · Jul 31, 2015 at 10:00 AM 0
Share

The enemies on level 2 Will target the player when the player Will be on level 2 ...

avatar image Hellium · Jul 31, 2015 at 10:01 AM 0
Share

In the Start function, the first person controller should have been instantiated, so does the "Health" game object. How and where do you instantiate these objects ?

avatar image Hellium · Jul 31, 2015 at 10:02 AM 1
Share

"The enemies on level 2 Will target the player when the player Will be on level 2 ..."

Then, the title and the question must be changed.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by puppeteer · Jul 31, 2015 at 01:54 PM

I think you should give the enemy script a way of knowing who the player is.

In the enemy script:

Define a variable that will hold a reference to the player object:

 public string playerTag = "Player";
 internal Transform playerObject;

And in the enemy's Start() or Awake() functions add this line:

 playerObject = GameObject.FindGameObjectWithTag(playerTag);

That should give all enemies a reference to the player in any scene. Now you can make the enemy attach playerObject.

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

7 People are following this question.

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

Related Questions

Enemy targeting player in different scene 1 Answer

Kill the Player? 1 Answer

Enemy does not look the player 2D platformer. 1 Answer

How can I load a scene when the enemy hits the player? 5 Answers

Enemy spawn then select target? 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