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

Application.Loadlevel not working?!?

Discussion in 'Scripting' started by phil2988, May 2, 2014.

  1. phil2988

    phil2988

    Joined:
    Apr 11, 2013
    Posts:
    23
    so here is my script:
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. function OnCollisionEnter ( collision : Collision )
    5.  
    6. {
    7.  
    8.     if(collision.gameObject.name == "Player")
    9.  
    10.     {
    11.  
    12.         Application.loadedLevel(0);
    13.  
    14.     }
    15.  
    16. }
    17.  
    what is wrong?
     
    Last edited: May 3, 2014
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824

    Please use Codetags [co.de] yourcode [/co.de] without the dots in the middle ofcourse.

    You have to write
    Code (csharp):
    1.  
    2. Application.LoadLevel(0);
    3.  
    as you've done it in the title, not loadedLevel as it is in your code since this one is a property and represents the current level's index.
     
  3. poncho

    poncho

    Joined:
    Dec 10, 2010
    Posts:
    65
    as Suddoha said, but you must be sure that your to load scene is in the build list, enter build configuration (crtl/cmmd + shift + b), and add the scene you want, check the index you got and use it in the LoadLevel method, also you can use the scene name
     
  4. phil2988

    phil2988

    Joined:
    Apr 11, 2013
    Posts:
    23
    okay thank you no errors.

    but the script doesnt work. the enemy witch have this script on it, is movin towards me and i want something to happen when it touches me. in this case i reload the level. but it just starts spinning around me....

    here is the enemy AI:
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class EnemyAI : MonoBehaviour {
    7.  
    8.     public Transform Player;
    9.     public float speed = 5;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         transform.LookAt(Player);
    19.         transform.position = transform.position + transform.forward * speed * Time.deltaTime;
    20.     }
    21. }
    22.  
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You either have to add the OnCollisionEnter function here and check the tag "Player" (or name in your case), or you have to change the Collision function on your player (which you had posted in your first post) so that it checks for the enemy's tag/name.
     
  6. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    As I know, collisions happen with physics only, so it wont work cuz you aren't using physics to move enemy object.
     
  7. phil2988

    phil2988

    Joined:
    Apr 11, 2013
    Posts:
    23
    so i need to somehow change the AI script?