Search Unity

Error while using FindWithTag inside of OnCollisionEnter

Discussion in 'Scripting' started by 2close4missiles, Apr 5, 2015.

  1. 2close4missiles

    2close4missiles

    Joined:
    Feb 13, 2015
    Posts:
    13
    So I am trying to make the player move to (0,0,0) when collided with an enemy. Here is my code:
    Code (CSharp):
    1.     void onCollisonEnter(Collision col)
    2.     {
    3.         if (col.gameObject.tag == "enemy")
    4.         {
    5.             transform.position = new Vector3 (0,0,0);
    6.         }
    7.     }
    This lead to a few errors:
    and
    as well as a few others that seemed to be saying the same thing. Can anyone translate for me? Please and thanks!
     
    Last edited: Apr 5, 2015
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    onCollisonEnter should have a capital or else it wont detect collision . So its OnCollisonEnter.

    For the errors, I cant see where they are coming from. Maybe your not showing the whole script?
     
    Last edited: Apr 6, 2015
  3. 2close4missiles

    2close4missiles

    Joined:
    Feb 13, 2015
    Posts:
    13
    Sure thing.
    Code (CSharp):
    1.    
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerScript : MonoBehaviour
    6. {
    7.     public float moveSpeed = 15;
    8.     public float sideSpeed = 10;
    9.     public float sprintSpeed = 20;
    10.     public float walkSpeed = 5;
    11.     public float jumpSpeed = 10;
    12.     public float rotateSpeed = 180;
    13.     public float gravity = 15;
    14.     public float moveSpeedSmooth = 0.045f;
    15.     public float sideMoveSpeedSmooth = 0.02f;
    16.     public float rotationSpeedSmooth = 0.305f;
    17.     public bool autoBounce = false;
    18.     float currentForwardSpeed;
    19.     float currentSideSpeed;
    20.     float forwardSpeedV;
    21.     float sideSpeedV;
    22.     float targetRotation;
    23.     float currentRotation;
    24.     float  rotationV;
    25.     CharacterController controller;
    26.     Vector3 currentMovement;
    27.     Transform enemy = GameObject.FindWithTag("Enemy").transform;
    28.  
    29.     void Start ()
    30.     {
    31.         controller = GetComponent<CharacterController>();    //For use instead of GetComponent<CharacterController>()
    32.     }
    33.    
    34.     void Update ()
    35.     {
    36.         WalkSprintParachute();
    37.  
    38.         targetRotation += Input.GetAxis ("Mouse X") * rotateSpeed * Time.deltaTime;
    39.         if(targetRotation > 360)
    40.         {
    41.             targetRotation -=360;
    42.         }
    43.         if(targetRotation < 0)
    44.         {
    45.             targetRotation +=360;
    46.         }
    47.         currentRotation = Mathf.SmoothDampAngle(currentRotation, targetRotation, ref rotationV, rotationSpeedSmooth);
    48.         transform.eulerAngles = new Vector3(0, currentRotation, 0);
    49.        
    50.         currentForwardSpeed = Mathf.SmoothDamp(currentForwardSpeed, Input.GetAxis("Vertical") * moveSpeed, ref forwardSpeedV,  moveSpeedSmooth);
    51.         currentSideSpeed = Mathf.SmoothDamp(currentSideSpeed, Input.GetAxis("Horizontal") * sideSpeed, ref sideSpeedV,  sideMoveSpeedSmooth);
    52.  
    53.         currentMovement = new Vector3(currentSideSpeed, currentMovement.y, currentForwardSpeed);
    54.         currentMovement = transform.rotation * currentMovement;
    55.  
    56.  
    57.         if (!controller.isGrounded)
    58.         {
    59.             currentMovement.y -= gravity * Time.deltaTime;
    60.         }
    61.         else
    62.         {
    63.             currentMovement.y = 0;
    64.         }
    65.  
    66.  
    67.         if (controller.isGrounded && !autoBounce)
    68.         {
    69.             if (Input.GetButtonDown("Jump"))
    70.             {
    71.                 currentMovement.y = jumpSpeed;
    72.             }
    73.         }
    74.         else if (controller.isGrounded && autoBounce)
    75.         {
    76.             if (Input.GetButton("Jump"))
    77.             {
    78.                 currentMovement.y = jumpSpeed;
    79.             }
    80.         }
    81.  
    82.  
    83.         controller.Move(currentMovement * Time.deltaTime);
    84.     }
    85.  
    86.     void OnCollisonEnter(Collision col)
    87.     {
    88.         if (col.gameObject.tag == "enemy")
    89.         {
    90.             transform.position = new Vector3 (0,0,0);
    91.         }
    92.     }
    93.  
    94.     public void WalkSprintParachute()
    95.     {
    96.         if (Input.GetButton("Parachute") && currentMovement.y <= 0 && !controller.isGrounded)
    97.         {
    98.             gravity = 2;
    99.             moveSpeed = 10;
    100.             rotateSpeed = 90;
    101.         }
    102.         else
    103.         {
    104.             gravity = 15;
    105.             rotateSpeed = 180;
    106.             if (Input.GetButton("Sprint"))
    107.             {
    108.                 moveSpeed = sprintSpeed;
    109.             }
    110.             else if (Input.GetButton("Walk") && controller.isGrounded)
    111.             {
    112.                 moveSpeed = walkSpeed;
    113.             }
    114.             else
    115.             {
    116.                 moveSpeed = 15;
    117.             }
    118.         }
    119.     }
    120. }
    121.  
    122.  
    Sorry, it's a bit messy.
     
  4. bostergsn

    bostergsn

    Joined:
    May 6, 2015
    Posts:
    1
    Transform enemy =GameObject.FindWithTag("Enemy").transform;

    Apparently unity doesn't like calling FindWithTag on static initializers. Try putting that in the Start function instead.