解析错误 - C#类 [英] Parsing error - C# classes
问题描述
我记得在javascript中编码并创建&访问对象非常简单。例如:
I remember coding in javascript and creating & accessing objects was fairly simple. For example:
var country = {
people:32,
homes:{
bigHomes:6,
smallHomes:2,
normalHomes:21
},
playgrounds:7,
countryName:"Towns Country"
}; // Then when accessing these variables...
alert("There are " + country.people + " people living in " + country.countryName + "!!");
我如何在C#中复制它?
我尝试过的事情:
我正在创造一个团结的游戏。在我的游戏中有单位,他们有不同的统计数据,例如,健康,伤害,每秒伤害等。
How do I replicate this in C#??
What I have tried:
I'm creating a game with unity. In my game there are units, and they have different stats, for example, health, damage, damage per second etc..
using UnityEngine;
using System.Collections;
public class Unit(float health, float speed, float damage, string name, float attackSpeed) {
this.health = health;
this.speed = speed;
this.damage = damage;
this.name = name;
this.attackSpeed = attackSpeed;
}
public class GameController : MonoBehaviour {
public float amountOfBasicBoat = 0;
public Unit BasicBoat = new Unit(99, 4, 15, "Basic Boat", 1.1);
}
我收到解析错误 - Assets / _Scripts / GameController.cs(4,18):错误CS8025:解析错误。
I get a parsing error - Assets/_Scripts/GameController.cs(4,18): error CS8025: Parsing error.
推荐答案
如果您不知道如何在c#中定义一个类,并且无法从谷歌中发现这样的基本内容那么我看不到你是如何有能力写游戏的。我建议你在c#上写一本书,然后在你尝试更高级的东西之前先学习它。您不能指望在论坛上发布问题来学习c#。
If you don't know how to define a class in c# and can't discover such a basic thing from google then I don't see how you have the ability to write a game. I'd advise you to get a book on c# and go through it to learn the basics before you try anything more advanced. You can't expect to learn c# from posting questions on a forum.
public class Unit
{
public float health { get; set; }
public float speed { get; set; }
public float damage { get; set; }
public float attackSpeed { get; set; }
public string name { get; set; }
public Unit(float health, float speed, float damage, string name, float attackSpeed)
{
this.health = health;
this.speed = speed;
this.damage = damage;
this.name = name;
this.attackSpeed = attackSpeed;
}
}
使用
Usage
public Unit BasicBoat = new Unit(99F, 4F, 15F, "Basic Boat", 1.1F);
您可以使用动态动态创建属性(就像在js中一样),但这不太可能满足您的总体要求。
You can create properties on the fly (as you do in js) using "dynamic" but that is not likely to meet your overall requirements.
这篇关于解析错误 - C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!