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

Some problem migrating to C#

Discussion in 'Scripting' started by U7Games, Apr 10, 2014.

  1. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Hey.. i have been using UnityScript for years.. but right now i want to change to C#

    the problem is that i got a lot of error, i don't understand them... what could be bad in my script?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class cs_Enemy_arquero : MonoBehaviour {
    6.  
    7.     private Transform camaraTransform;
    8.     private CharacterController controlador;
    9.     private Vector3 direccion;
    10.     private Transform jugador;
    11.     private Vector3 rotacion;
    12.     private bool disparando = false;
    13.  
    14.     bool activado = true;
    15.  
    16.     void Awake ()
    17.     {
    18.         camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent(Transform);
    19.         controlador = GetComponent(CharacterController);
    20.         jugador = GameObject.FindWithTag("Player").GetComponent(Transform);
    21.    
    22.     }
    23.  
    24.  
    25.     void Update ()
    26.     {
    27.         if (controlador.isGrounded)
    28.         {
    29.             if (Vector3.Distance(transform.position, jugador.position) < 20)
    30.             {
    31.                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(jugador.position - transform.position), Time.deltaTime * 7);
    32.                 direccion = transform.TransformDirection(Vector3.forward * 7);
    33.                 direccion *= 50 * Time.deltaTime;
    34.             }
    35.        
    36.             else if (Vector3.Distance(transform.position, jugador.position) < 15)
    37.             {
    38.                 disparando = true;
    39.                 Debug.Log("Dispara flecha!");
    40.                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(jugador.position - transform.position), Time.deltaTime * 7); 
    41.                 direccion = transform.TransformDirection(Vector3.forward * 0);
    42.                 direccion *= 0 * Time.deltaTime;
    43.                
    44.             }          
    45.            
    46.             //Si está lejos entonces detenerse
    47.             else
    48.             {
    49.                 direccion = transform.TransformDirection(Vector3.forward * 0);
    50.                 direccion *= 0 * Time.deltaTime;
    51.             }
    52.            
    53.             transform.rotation.x = 0;
    54.             transform.rotation.z = 0;
    55.            
    56.             if (controlador.velocity.magnitude > .1)    animation.CrossFade("anim_Enemy_arquero_run");
    57.             else
    58.             {
    59.                 if (!disparando)    animation.CrossFade("anim_Enemy_arquero_idle");
    60.             }
    61.            
    62.         }
    63.        
    64.         direccion.y -= 140 * Time.deltaTime;
    65.         controlador.Move(direccion * Time.deltaTime);
    66.     }
    67. }
    68.  


    Im really new to C# ... my apologies..
    Thanks.
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Well, one of your errors is because you are attempting to pass a type as a variable:
    Code (csharp):
    1.  
    2. camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent(Transform);
    3.  
    With C# I would highly recommend using the generic approach:
    Code (csharp):
    1.  
    2. camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent<Transform>();
    3.  
    I hope that this helps to get you started with your errors :)
     
  3. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Hey Thanks!.. that solved the problem...

    so.. everytime i want to get a component i must to use < and > before (); ?? is that different?

    There still are two errors .. in

    Code (csharp):
    1.  
    2. transform.rotation.x = 0;
    3. transform.rotation.z = 0;
    4.  
    how to solve that?
    Thanks friend.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    .GetComponent<Transform>() or .GetComponent(typeof(Transform)) is basically the same thing.

    Useful thing for your to google;

    typeof()
    .GetType()
    Generic Methods and Generic Types

    As for that specific error;

    Code (csharp):
    1.  
    2. Vector3 position = transform.position;
    3. position.x = 0;
    4. transform.position = position
    Structs are passed around as copies, so you have to take a copy to edit before sending it back.
    I would also advice not editing the component of a quaternion unless you know exactly what you are doing.
     
  5. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    You need to use a temporary variable:
    Code (csharp):
    1.  
    2. var rotation = transform.rotation;
    3. rotation.x = 0;
    4. rotation.z = 0;
    5. transform.rotation = rotation;
    6.  
    Though I am not sure you really want to be adjusting Quaternions in that way...
     
  6. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Thanks for answering LightStrike..

    I tried with
    Code (csharp):
    1.  
    2. camaraTransform = GameObject.FindWithTag ("MainCamara").GetType(Transform);
    3.  
    4. //and
    5.  
    6. camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent(typeof(Transform));
    7.  
    8.  
    but none of them work ....

    i´m getting mad with c# haha..
    excuse me for my foolishness with c#
     
  7. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    ah okey.. i do that to prevent enemy looking at player while player jumps ...
     
  8. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I would go with the generic version like I suggested before. The old approach is this:
    Code (csharp):
    1.  
    2. camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent(typeof(Transform)) as Transform;
    3.  
     
  9. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    You googled nothing of what I listed you, didn't you? Do you just type stuff without knowing what they do?
     
  10. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    numberkruncher thanks.. ill use that..

    LightStriker, i´m sorry, i said that i´m ultra novice with c#.. but i will investigate.. thanks..
     
  11. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    Alternatively and much easier to read, if your MainCamara is your Main Camera, you can access your camera's transformation as follows:

    Code (csharp):
    1.  
    2. camaraTransform = Camera.main.transform;
    3.