Some problem migrating to C#

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?

using UnityEngine;
using System.Collections;

public class cs_Enemy_arquero : MonoBehaviour {

	private Transform camaraTransform;
	private CharacterController controlador;
	private Vector3 direccion;
	private Transform jugador;
	private Vector3 rotacion;
	private bool disparando = false;

	bool activado = true;

	void Awake ()
	{
		camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent(Transform);
		controlador = GetComponent(CharacterController);
		jugador = GameObject.FindWithTag("Player").GetComponent(Transform);
	
	}


	void Update ()
	{
		if (controlador.isGrounded)
		{
			if (Vector3.Distance(transform.position, jugador.position) < 20)
			{
				transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(jugador.position - transform.position), Time.deltaTime * 7);
				direccion = transform.TransformDirection(Vector3.forward * 7);
				direccion *= 50 * Time.deltaTime;
			}
		
			else if (Vector3.Distance(transform.position, jugador.position) < 15)
			{
				disparando = true;
				Debug.Log("Dispara flecha!");
				transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(jugador.position - transform.position), Time.deltaTime * 7);	
				direccion = transform.TransformDirection(Vector3.forward * 0);
				direccion *= 0 * Time.deltaTime;
				
			}			
			
			//Si está lejos entonces detenerse
			else 
			{
				direccion = transform.TransformDirection(Vector3.forward * 0);
				direccion *= 0 * Time.deltaTime;
			}
			
			transform.rotation.x = 0;
			transform.rotation.z = 0;
			
			if (controlador.velocity.magnitude > .1)	animation.CrossFade("anim_Enemy_arquero_run");
			else
			{ 
				if (!disparando)	animation.CrossFade("anim_Enemy_arquero_idle");
			}
			
		}
		
		direccion.y -= 140 * Time.deltaTime;
		controlador.Move(direccion * Time.deltaTime);
	}
}

Im really new to C# … my apologies…
Thanks.

Well, one of your errors is because you are attempting to pass a type as a variable:

camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent(Transform);

With C# I would highly recommend using the generic approach:

camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent<Transform>();

I hope that this helps to get you started with your errors :slight_smile:

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

transform.rotation.x = 0;
transform.rotation.z = 0;

how to solve that?
Thanks friend.

.GetComponent() 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;

Vector3 position = transform.position;
position.x = 0;
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.

You need to use a temporary variable:

var rotation = transform.rotation;
rotation.x = 0;
rotation.z = 0;
transform.rotation = rotation;

Though I am not sure you really want to be adjusting Quaternions in that way…

Thanks for answering LightStrike…

I tried with

camaraTransform = GameObject.FindWithTag ("MainCamara").GetType(Transform);

//and

camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent(typeof(Transform));

but none of them work …

i´m getting mad with c# haha…
excuse me for my foolishness with c#

ah okey… i do that to prevent enemy looking at player while player jumps …

I would go with the generic version like I suggested before. The old approach is this:

camaraTransform = GameObject.FindWithTag ("MainCamara").GetComponent(typeof(Transform)) as Transform;

You googled nothing of what I listed you, didn’t you? Do you just type stuff without knowing what they do?

numberkruncher thanks… ill use that…

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

Alternatively and much easier to read, if your MainCamara is your Main Camera, you can access your camera’s transformation as follows:

camaraTransform = Camera.main.transform;