• 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 11, 2013 at 08:10 PM · yieldactive

Script to set character to active after 3 seconds not working

Hello,

When my character controller walks into the collider around an npc: the npc is set to inactive, and a new npc is set to active in the same position. After 3 seconds I want the second npc to be set to inactive and the first npc to be set to active again in the new position. (So in the game looking like a character walking past and for three seconds appearing as a different person)

The script works in that on collision, the first npc is set to inactive and the second npc set to active in the right position.

The problem is that after 3 seconds - he doesn't switch back to the first character, and I can't work out why.

This is the script (Javascript):

 var player : GameObject;
 var dan : GameObject;
 var exoGrey : GameObject;
 var characterPosition : Vector3;
 
 function Start () {
 
 exoGrey.SetActive (false);
 dan.SetActive (true);
 
 }
 
 
 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;
 
 }

Does anyone have any idea why? (I'm not getting any errors in the console)

Thanks, Laurien

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 ExTheSea · Jun 11, 2013 at 08:15 PM 0
Share

Here are a few questions i have:

Is the exoGrey variable properly assigned?

To what gameobject is the script attached?

avatar image laurienvictoria · Jun 11, 2013 at 09:34 PM 0
Share

I've attached the script to the collider, and the exoGrey variable has been properly assigned - for the first part of the script does work (in setting him to active).

I've tried a lot of things out - and it seems that when I use yield WaitForSeconds in a function OnTriggerEnter it doesn't work.

For example:

 var carl : GameObject;
 
 function OnTriggerEnter(other: Collider){
 
     if (other.gameObject.tag == "Player")
     
     yield WaitForSeconds (4);
     
     Destroy (carl);
     
     }

destroys carl immediately - rather than after 4 seconds.

Why is this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lockstep · Jun 11, 2013 at 11:30 PM

Big edit:

I was wrong about the return type of OnTriggerEnter. It can indeed be of type IEnumerator. I still wouldn't recommend it though. I tried the following script:

 function OnTriggerEnter () {
     Debug.Log("foo " + Time.time);
     yield WaitForSeconds(3.4);
     Debug.Log("bar " + Time.time);
 }

And got: foo 0.48 bar 3.897639 Which means the yield gets executed correctly. The reason your short example did not yield, is that you didn't use curly brackets and thus the if statement only was relevant for the single line directly under it.

Back to your original problem. Make sure, that the script is not on a gameObject, which gets disabled. Since it works otherwise, this is pretty much the only thing that could go wrong.

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 laurienvictoria · Jun 12, 2013 at 11:41 AM 0
Share

Oh the scripting reference confused me - because it says OnTriggerEnter can be a co-routine, simply use the yield statement in the function.

I tried the new code, but it still destroys the character immediately. Do you know what I'm doing wrong?

 var carl : GameObject;
 
 function OnTriggerEnter(other: Collider){
 
 StartCoroutine (CharacterSwap());
 
 }
 
 function CharacterSwap () {
 
     if (gameObject.tag == "Player")
     
     yield WaitForSeconds (4);
     
     Destroy (carl);
     
     }
     
avatar image Leslie-Young · Jun 12, 2013 at 12:14 PM 0
Share

According to, http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter.html """OnTriggerEnter can be a co-routine, simply use the yield statement in the function."""

It is strange that the Destroy still happens instant, even after specifically doing a StartCoroutine call. Perhaps make the delay a little longer, like 60 seconds, just to be 100% sure you can see what is happening there.

avatar image laurienvictoria · Jun 12, 2013 at 12:17 PM 0
Share

I've worked it out: I needed to put the yield function before the if (other.gameObject.tag == "Player"). I didn't realise that.

This script works now:

 var carl : GameObject;
 var check;
 
 function Start () {
 
 carl.SetActive (true);
 
 }
 
 function OnTriggerEnter(other: Collider){
 
 yield WaitForSeconds (4);
 
     if (other.gameObject.tag == "Player")
     
     
     
     carl.SetActive (false);
     
     }

I'm still struggling to work out why the original script posted in the question doesn't work though.

avatar image Jamora · Jun 12, 2013 at 06:42 PM 1
Share

I would assume ExTheSea meant this code snippet, where the tag of the other gameobject is checked.

 function OnTriggerEnter(...){
      
     if (other.gameObject.tag == "Player"){
         yield WaitForSeconds (4);
         Destroy (carl);
     }
      
 }
avatar image ExTheSea · Jun 12, 2013 at 06:49 PM 1
Share

Yes i meant that part like Jamora pointed out.

@laurienvictoria: Hmm for some reason i think that you may disable the object the script is attached to in the part before the yield. This way the rest doesn't get executed.

Show more comments

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

19 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

Related Questions

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

A node in a childnode? 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

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

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