• 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 laurienvictoria · Jun 10, 2013 at 11:10 PM · activerange

Set npc to inactive when certain distance away from controller (script not working)

Hello - when an npc walks past my character controller: when he is a certain distance away from the character controller , I want the npc to be set to inactive and another npc to be instantiated in the same position. After three seconds I want this second npc to be set to inactive, and the first npc to be set to active in the new position.

The problem I have is that although I can set the characters to active in the right position - the range part of my script doesn't seem to be working - and when my character controller walks up to the npc - he isn't set to inactive.

Could you tell me what's wrong with my script? (JavaScript)

 var player : GameObject;
 var dan : GameObject;
 var exoGrey : GameObject;
 var characterPosition : Vector3;
 var range : float = 4;
 
 function Start () {
 
 dan.SetActive (false);
 exoGrey.SetActive (false);
 
 }
 
 function OnTriggerEnter () {
 
 
 dan.SetActive (true);
 
 var distance = Vector3.Distance(player.transform.position, transform.position); 
 
 if (distance <= range){ 
  
 characterPosition = dan.transform.position;
 dan.SetActive (false);
 exoGrey.SetActive (true);
 exoGrey.transform.position = characterPosition;
 exoGrey.transform.Rotate (new Vector3(0, 180, 0));
 
 
 yield WaitForSeconds (3);
 
 characterPosition = exoGrey.transform.position;
 exoGrey.SetActive (false);
 dan.SetActive (true);
 dan.transform.position = characterPosition;
 
 }
 
 }

Thanks so much, Laurien

Comment
Add comment · Show 3
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 iwaldrop · Jun 11, 2013 at 06:29 AM 1
Share

Why don't you set the radius of your collider to equal the distance that you want it to trigger at? That would alleviate the distance check. And what is supposed to be set to active when you enter the trigger? The code says exo grey will be active for 3 seconds, and then Dan will be left active thereafter.

avatar image laurienvictoria · Jun 11, 2013 at 02:15 PM 0
Share

Thanks - I've attached a collider to Dan ins$$anonymous$$d with this script attached to it. But although he is set to inactive when the player hits Dan's collider, and exo grey is set to active. The yield WaitFor Seconds doesn't work, and exo grey isn't set to inactive for some reason. Do you know why?

 var player : GameObject;
 var dan : GameObject;
 var exoGrey : GameObject;
 var characterPosition : Vector3;
 
 
 function OnTriggerEnter () {
 
 
 characterPosition = dan.transform.position;
 dan.SetActive (false);
 exoGrey.SetActive (true);
 exoGrey.transform.position = characterPosition;
 
 
 
 yield WaitForSeconds (3);
 
 characterPosition = exoGrey.transform.position;
 exoGrey.SetActive (false);
 dan.SetActive (true);
 dan.transform.position = characterPosition;
 
 }
avatar image iwaldrop · Jun 11, 2013 at 03:15 PM 0
Share

Are you sure that there are no errors in your console? I bet the yield does work through, because it works everywhere else in unity on everyone else's machine. ;) try adding a debug.log statement in your code after your yield to see if it prints out. Learning to debug your code is more important to being a successful programmer than learning how to code in the first place.

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

15 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

Related Questions

carl.transform.position = characterPosition -- how to set to this specific position but -3 on x axis? 2 Answers

Script to set character to active after 3 seconds not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Set npc to active in same position as previous npc (script not working) 1 Answer

A node in a childnode? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges