Jump to content
  • Advertisement
Zebura

Unity C# Basics learning

Recommended Posts

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);
		}
	}
}

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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#.

Share this post


Link to post
Share on other sites
Posted (edited)

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.

Edited by Nypyren

Share this post


Link to post
Share on other sites
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

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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. :) 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!