Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Basic Script Doesn't Work.

Discussion in 'Scripting' started by 420BlazeIt, Nov 15, 2014.

  1. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I have an object in my game which needs to move back and forward.

    This is my script:
    Code (JavaScript):
    1.  
    2. function Start () {
    3.     InvokeRepeating("PlayAnims", 0, 2);
    4. }
    5.  
    6. function PlayAnims () {
    7.     animation.CrossFade("Back");
    8.     yield WaitForSeconds (1);
    9.     animation.CrossFade("Forward");
    10.     yield WaitForSeconds (1);
    11. }
    But this doesn't do anything. I don't get errors and the object won't move.
    Please help :(
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you make sure that all the parts in the code are reached repeatedly, e.g. by placing some Debug.Logs?
     
  3. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I don't understand why this won't work.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I don't understand it too. That's why we need to find out what is going on. Place some Debug.Log ("..."); in your code, such that you see what is actually being executed. Knowing that the code is actually being executed is the first step to isolate the issue.
     
  5. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I did this:
    Code (JavaScript):
    1.  
    2. function Start () {
    3.     InvokeRepeating("PlayAnims", 0, 2);
    4. }
    5.  
    6. function PlayAnims () {
    7.     Debug.Log("Play Back Animation");
    8.     //animation.CrossFade("Back");
    9.     yield WaitForSeconds (1);
    10.     Debug.Log("Play Back Animation");
    11.     //animation.CrossFade("Forward");
    12.     yield WaitForSeconds (1);
    13. }
    Nothing printed to the console...
     
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    put the script on the object that you want to animate.
    Also put #pragma strict at the top of your code.
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It might be that invoke repeating doesn't like coroutines. You may try this instead:

    Code (csharp):
    1. function Start () {
    2.   StartCoroutine (PlayAnims ());
    3. }
    4. function PlayAnims () {
    5.   while (true) {
    6.   Debug.Log("Play Back Animation");
    7.   //animation.CrossFade("Back");
    8.   yield WaitForSeconds (1);
    9.   Debug.Log("Play Back Animation");
    10.   //animation.CrossFade("Forward");
    11.   yield WaitForSeconds (1);
    12.   }
    13. }
     
  8. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    This seems to work but I get two errors:

    Loading script assembly "Library/ScriptAssemblies/Assembly-CSharp-firstpass.dll" failed!
    Loading script assembly "Library/ScriptAssemblies/Assembly-UnityScript-firstpass.dll" failed!
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You most likely get them in MonoDevelop. Sorry, can't help with that, because I usually don't use UnityScript.
     
  10. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I also get
    "The referenced script on this Behaviour is missing!"
     
  11. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Put the script on the object...
     
  12. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    That means Unity "lost" the "connection" to a script and you need to reassign it!
    CLICK ON THE WARNING MESSAGE. THIS WILL SELECT THE GAME OBJECT IN THE HIERARCHY WHERE THE SCRIPT NEEDS TO BE REASSIGNED.
     
    zDemonhunter99 likes this.
  13. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    I like how he's been ignoring my solution the entire time.

     
  14. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    If that is your entire script it's never going to move anyway.
    All you've told it to do is play those animations.
     
    djfunkey likes this.