Unity C# Basics learning

Started by
10 comments, last by Rutin 4 years, 7 months ago

Hi everyone. I basically just got Unity, and I'm just starting out with scripting with c#, or rather, I am utterly lost, heh.

 

I could use some assistance. I'm doing a very basic 2D project to just learn how to code. And I have some trouble, I am getting a compiling error and I don't know what's wrong:

 


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player1_Controls : MonoBehaviour {

	public float speed = 10f;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
	

		float Dirx = Input.GetAxis ("Horizontal") * speed * Time.deltaTime;

		void SetTransformX(float n){
			transform.position = new Vector3(n, transform.position.y, transform.position.z);
		}
		void Update (){
			SetTransformX(7.0f);
		}
	}
}

 

Advertisement
8 minutes ago, Zebura said:

I am getting a compiling error

Include the actual error, copy & paste it.

 

EDIT: Actually, I saw it.

In C#, you cannot have functions inside other functions. Move SetTransformX and Update outside of Update.

Also, You can't have 2 functions with the same name.

Hello to all my stalkers.

Ahh. I see. Thank you for pointing that out, I got that part working now.

When you're posting about compile errors it would help to post the actual error message you're receiving.

Is there a reason you're not doing this:


private float speed = 10f;
    private float Dirx = 0f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Dirx = Input.GetAxis("Horizontal") * speed * Time.deltaTime;

        SetTransformX(7.0f);
    }

    // ...
    void SetTransformX(float n)
    {
        transform.position = new Vector3(n, transform.position.y, transform.position.z);
    }

In your code you posted you shouldn't make a habit of re-declaring variables in your update method. Declare it once and update as needed. Also is there a reason you need to set your transform every tick to the same position?

Programmer and 3D Artist

I will do that in the future, the reason was I was eager to get input.
The second reason for transform in every tick, I am pretty much just starting out with c#.

I disagree with declaring Dirx as a member variable.  In my experience, limiting variables to the narrowest possible scope keeps code cleaner and reduces the possibility of introducing bugs.

5 minutes ago, Zebura said:

I will do that in the future, the reason was I was eager to get input.
The second reason for transform in every tick, I am pretty much just starting out with c#.

No problem! :) 

Here are some good resources for C#:

https://docs.microsoft.com/en-us/dotnet/csharp/

https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/intro-to-csharp/

Also, a good book:

https://www.amazon.com/C-7-0-Nutshell-Definitive-Reference/dp/1491987650/ref=zg_bs_697342_6?_encoding=UTF8&psc=1&refRID=2NSN23J574P1P9A2X2ST

You'll want to also review C# 8 as well:

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8

 

Programmer and 3D Artist

That is very helpful! Just what I needed, thanks for the reading material, much appreciated!

10 minutes ago, Nypyren said:

I disagree with declaring Dirx as a member variable.  In my experience, limiting variables to the narrowest possible scope keeps code cleaner and reduces the possibility of introducing bugs.

Local variables are fine, however I have no idea if this person is planning to use that variable anywhere else and has not indicated such. When you see new programmers with unity declaring all their components in Update and calling them it's a real performance issue. It's all about building good habits. If the variable makes sense to be local based on the implied usage then it is fine. :) 

Programmer and 3D Artist

I agree that Component references should be cached as members, but a float isn't a Component.  We need to be careful to provide advice which makes sense for the specific situation.

This topic is closed to new replies.

Advertisement