so I am slowly reading through the book "The C# players guide, second edition." and I have gotten to a Try it Out! activity involving classes. It tells me to create a Color class with getters and setters for red, blue, green and alpha, which I have done.
It also told me to make a ball class, which will require size, Color (using the color class) and timesThrown variables. I have done that fine and I am now back in the main Program class making instances of the ball class.
Really I am just curious as to what is a better way to go about making this ball because one way looks more efficient, but then I am not sure how to use the getters and setters of the Color class, and the other way looks less efficient but allows me to use all of that. And without being able to use the methods in the class I don't see why i wouldn't just define the colors within the Ball class anyway, unless just for the sake of learning about classes and how to make them, this tutorial has had me do it this way?
So the way I personally did it first, which let me use all the methods in the Color class was like so:
Color red_color = new Color(255, 0, 0);
Ball red_ball = new Ball(5, red_color);
because that lets me do red_color.GetRed(); for example.
The other way is this:
Ball blue_ball = new Ball(4, new Color(0, 0, 255));
However when I do this way, how am I supposed to use the methods defined within the class Color? My first instinct was to use Static as I am not making a instance, but obviously for example, red, isn't always going to be the same value for each ball so that wouldn't work.
So yeah, does the second approach allow to me use the Colors methods at all? Apologies if I worded myself poorly.
In your ball class you can add a property called color like this
Color BallColor {get;private set;}
and in your constructor you should set the ball color you want
Ball blue_ball = new Ball(4, new Color(0, 0, 255));
and the you can use the color methods like this
blue_ball.BallColor.GetRed();
If the only place you'll need to interact with that instance of Color is inside that instance of Ball, do it the second way.
However, if you'll need to interact with that instance of Color someplace else, do it the first way. For example, if you're creating four red Balls in a row, create one red Color instance and pass it to each of them:
var balls = new List<Ball>();
var red = new Color(255, 0, 0);
for (int i = 0; i < 4; ++i) {
balls.Add(new Ball(4, red));
}
Sometimes one is appropriate, sometimes the other. Neither is absolutely "best"; do what works in the specific case you've got in front of you. Sometimes you might create a local variable just to aid readability.
See #ShlomiHaver's answer as well.
My first instinct was to use Static as I am not making a instance
Well you're making an instance. Every time you see a new keyword that's a new instance. You just don't have a reference for it as you passed it directly to the constructor:
new Ball(4, new Color(0, 0, 255));
If you have a public BallColor Color { set; get; } inside the Ball class then Color is accessible through the Ball object like this :
red_ball.BallColor.Something
So, if you just need the Color object to pass it to the constructor of Ball , use the second method. If you need a reference to Color in order to read/write data to/from that object in the code that follows in your program, then use the first approach.
There is no rule here, it is based on the code that follows and the usage. So you need to ask yourself :
Do I need to keep a reference to the Color object outside my Ball object ?
You certainly can do this, but remember that the context of an instantiated object is always the most important thing.
You want to know the color of the ball, right? So add a suitable method to the Ball class.
public class Ball
{
private int Size { get; set; }
private Color BallColor { get; set;}
Ball(int size, Color color)
{
Size = size;
BallColor = color;
}
public Color GetBallColor()
{
return BallColor;
}
}
Then you can use the ball's color like this:
var ball = new Ball(5, new Color(0, 0, 0));
var ballColor = ball.GetBallColor();
var ballColorRedValue = ballColor.GetRed();
Achieving the same thing using properties is absolutely acceptable.
Related
First of all: I'm quite new to unity and c#, so please be nice if I ask dumb questions.
I'm trying to make a game like minesweeper. So I want an easy grid with covered tiles and if you klick on one, it opens up.
I use a main script I create a grid like so:
private void CreateTieleSet(int width, int height){
_tiles = new Dictionary<Vector2, Tile>();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
var spawnedTile = Instantiate(_tilePrefab, new Vector3(x, y), Quaternion.identity);
spawnedTile.name = $"Tile {x} {y}";
spawnedTile.status = field[x, y];
_tiles[new Vector2(x, y)] = spawnedTile;
}
}
}
I use the function
public Tile GetTileAtPosition(Vector2 pos)
{
if(_tiles.TryGetValue(pos, out var tile))
{
return tile;
}
return null;
}
to get the tile at the position xy, but I can't use it since
Tile tempTile = GetTileAtPosition(new Vector2(x, y));
tempTile.ChangeSprite(field[x, y]); //example-funktion from tile-script
allways results in NullReferenceExeption-Error. I know the probleme, since I allways struggle with using scripts from other tiles. Bus usually I can use [SerializeField] Tile... and than dragdrop it onto it. In this case however I obvioulsy can't do that.
Btw: I realy tried to solve this problem with other solutions found online, but everyone has complete different ideas how to do it and nothing seems to work for me :/
Your best bet for this one is going to be using Unity's built-in Tilemaps - you'll want to take your sprites for Minesweeper and add them to a TilePalette, and paint them onto a GameObject with a Grid component, with children GameObjects holding a Tilemap component - one per layer. Then, when you want to change the sprite, you can do something like the following:
using UnityEngine;
using UnityEngine.Tilemaps;
class GridManager
{
[SerializeField] Tilemap gridDisplayLayer;
public void ChangeSpriteAtLocation(Vector3Int cellLocation, Tile tileToChangeTo)
{
gridDisplayLayer.SetTile(cellPosition, tile);
}
}
If you want the simplest answer, just add Tile members to the class above for swapping between them and call ChangeSpriteAtLocation during runtime. Might need to change the flags on the Tilemap to change the sprite and such, but I think that should be good enough to get you started :) When you generate the TilePalette, Unity will give you Tile assets to save that you can then reference for the above example method.
I'm still getting all the intricacies of Tilemaps myself, though, and I'm definitely more design minded, so I won't be surprised if someone has a more succinct or efficient solution.
Hope this helps get you started, though!
Vellv
private Color solveColor;
void Start()
{
Color[] colors = { Color.cyan, Color.red, Color.green, new Color(245, 195, 29), Color.yellow, Color.magenta };
int lengthOfColors = colors.Length;
int solveColor = UnityEngine.Random.Range(0, lengthOfColors);
}
private void start()
{
GetComponent<MeshRenderer>().material.color = solveColor;
}
private void FixedUpdate()
{
// Set the balls speed when it should travel
if (isTraveling) {
rb.velocity = travelDirection * speed;
}
// Paint the ground
Collider[] hitColliders = Physics.OverlapSphere(transform.position - (Vector3.up/2), .05f);
int i = 0;
while (i < hitColliders.Length)
{
GroundPiece ground = hitColliders[i].transform.GetComponent<GroundPiece>();
if (ground && !ground.isColored)
{
ground.Colored(solveColor);
}
The above code is supposed to pick one color from the colors array and assign it to both the ball and balls painting ability (whenever the ball collides with the ground it changes its color) however the paint the ball leaves is always black and the ball itself is always orange (pretty sure the ball color is coming from its default). I can't figure out why this is happening any help is very appreciated.
Thank you for your time
In the code you provided, nowhere do you set the material color of the ball again aside from Start. If you want to have the particles behind the ball leave different colors, you will need to instantiate a new instance of the material. The reason for this is because materials in Unity are default shared between all instances of that particular material.
All of this info and a bit more can be found on the Material docs page.
As you have a fixed size of colors you are using, I would instead create 6 new materials and make an array of materials instead. Now, instead of randomly picking a color, pick a material and assign it to the ball or your new instanced painting ability. I am also confused as to why you are placing your array of colors inside of your Start function. It would be localized to that function only then. You also appear to have two Start functions, which is odd. One being the Monobehaviour Start and another start. Unless that is intended, your second start will not be run unless you call it.
Now to get to the solution I was talking about.
// assign these in the inspector to your new materials
[SerializeField] private List<Material> materials = new List<Material>();
private MeshRenderer meshRender;
private void Start()
{
meshRenderer = GetComponent<MeshRenderer>();
// set our first random Material
SetNewMaterialColor();
}
private void SetNewMaterialColor()
{
meshRenderer.material = GrabNewMaterial();
}
private void FixedUpdate()
{
// Set the balls speed when it should travel
if (isTraveling) {
rb.velocity = travelDirection * speed;
}
// Paint the ground
Collider[] hitColliders = Physics.OverlapSphere(transform.position - (Vector3.up/2), .05f);
int i = 0;
while (i < hitColliders.Length)
{
GroundPiece ground = hitColliders[i].transform.GetComponent<GroundPiece>();
if (ground && !ground.isColored)
{
// change this from a color to a material instead
ground.Colored(meshRenderer.material);
// set a new material to your main object
SetNewMaterialColor();
}
}
}
private Material GrabNewMaterial()
{
return materials[UnityEngine.Random.Range(0, materials.Count)];
}
You will need to change your Colored function to take in a Material instead of a Color. If you want the implementation to be more dynamic, you can instead create an instance of your material and set the color dynamically, but as you have a fixed size of colors I do not think you need to do that.
Edit: The one other option which involves creating a new shader would be to utilize [PerRendererData] meaning each object for a property field is rendered individually. I would go with the previous option as either option using shaders or instanced materials is a bit more complex.
You would need to use a MaterialPropertyBlock and can then assign the color when you want. It would look something like
public void SetNewColor()
{
// create a new material property block
MaterialPropertyBlock tmpBlock = new MaterialPropertyBlock();
// grab the current block from our renderer
meshRender.GetPropertyBlock(tmpBlock);
// set our changes to the block
tmpBlock.SetColor("_Color", YourColorHere);
// now apply our changes
tmpRend.SetPropertyBlock(tmpBlock);
}
And you would need to create a new shader that laters the Main Color property by using the PerRendererData attribute.
Properties
{
[PerRendererData]_Color("Main Color", Color) = (1,1,1,1)
...
Also, one other question I have is why you are using Physics.OverlapSphere instead of just an OnCollisionEnter? Or if your game is 2D, then OnCollisionEnter2D and let the physics engine handle how collisions work, then just change the colors when the collision occurs?
Edit: Here are the answer to your questions - let me know if you have more.
In the line "[SerializeField] private List materials = new
List();" which section do I need to replace with the
materials and how?
The line as is is fine. By using [SerializeField] it exposes this list to the editor. You will want to create several new duplicate materials that use your 6 different colors. Instead of setting the colors, you will be setting materials now. What I mean by inspector and editor is you can find the object that has this script on it in Unity, select it (it must be a Prefab or in the scene), then a tab of the Unity editor will populate with information about this object. Find the script portion and find the field materials. There should be a drop-down arrow, click it and set the number to 6 (or however many material swaps you want). Now create 6 new materials with your colors and drag them into the boxes that appeared.
Would it be something like writing "./Materials/Ball 1" in the () for
example?
Nope! You would be assigning this data in the inspector, so the data would be stored in the list without referencing them in code.
And I'm not sure how to assign this to my ball using "[SerializeField]
private GameObject paintObject = null;"
Similarly, this would appear in the inspector. However, remove this line as I misunderstood your original question and accidentally left this in. I assumed that your paint object was a Prefab that you were spawning after the ball bounced, not the ground that you were changing the color of.
I get the error "Argument 1: cannot convert from
'UnityEngine.Material' to 'UnityEngine.Color'"
Yep! So as I mentioned in the comments, your function call to your paint object is most likely currently taking a Color parameter. As I changed your implementation to instead directly set Material, you will need to change how that function signature. Specifically the line:
ground.Colored(meshRenderer.material);
You have some object ground that is of type GroundPiece and has a function called Colored. I assume it currently look something like:
public void Colored(Color color){...}
You want to change this instead to:
public void Colored(Material mat{...}
After changing it, instead of changing the ground's color in this script, you would change its material directly. Let me know if you have more questions.
I have a space shooter where players can select a color for their ships, and this applies to the bullets the ships shoot as well. The ships use the same bullet prefab, so I would like to change the color of the bullet once it's created in the game. I originally used the following code to change the color.
Material bulletMat = this.GetComponent<MeshRenderer>().sharedMaterial;
if (bulletMat != null)
{
bulletMat.SetColor("_TintColor", color);
}
However, I found out that this will change the color of the material and the bullets would switch in between colors with each bullet created. After doing some more research, I came accross the MaterialPropertyBlock variable and the following tutorial http://thomasmountainborn.com/2016/05/25/materialpropertyblocks/. So I followed this tutorial and setup the following code.
public Renderer renderer;
public MaterialPropertyBlock matBlock;
public Color color;
public bool colorSet = false;
void Awake()
{
renderer = this.GetComponent<Renderer>();
matBlock = new MaterialPropertyBlock();
}
public void ColorSet(Color color)
{
this.color = color;
this.colorSet = true;
}
void Update()
{
if (this.colorSet)
{
renderer.GetPropertyBlock(matBlock);
matBlock.SetColor("_Color", this.color);
renderer.SetPropertyBlock(matBlock);
}
}
However, the same issue as the previous solution happened....
Can someone help me figure out how to set the color for each individual bullet?
You use sharedMaterial only wherever you really want to change the material for every Object using this material. That's exactly what you don't want to do. Instead you ahve to use material so you only change a "clone" instance of the material.
Unity actually automatically instantiates a local copy of the current material only for that object if you do:
public class ColorChanger : MonoBehavior
{
private MeshRenderer meshRenderer;
private void OnEnable()
{
meshRenderer = GetComponent<MeshRenderer>();
}
public void SetColor(Color newColor)
{
renderer.material.color = newColor;
}
}
or using SetColor as you had it should do the same I guess
public void SetColor(Color newColor)
{
renderer.material.SetColor("_TintColor", newColor);
}
I personally find the before metnioned one easier. But the second one gives you more control since you can e.g. also change the emitColor which is not possible on another way.
Now when you instantiate your bullets make sure you first instantiate the prefab and than change the color using the component above:
public GameObject bulletPrefab;
public Color myColor;
public GameObject Hand;
private void Shoot()
{
// There are various ways for Instantiate e.g. providing start position and rotation or also the parent object
// Since it shall be fired from the player I'll assume here there is a "Hand" object from which you want to fire
// and that you want to add the bullet directly to the hierarchy without a parrent
GameObject newBullet = Instantiate(bulletPrefab, Hand.position, Hand.rotation);
// Now we get the color change component
ColorChanger colorChanger = newBullet.GetComponent<ColorChanger>();
// And set the color
colorChanger.SetColor(myColor);
}
Note:
In case this is a multiplayer game the bullets should be Instantiated only on the Server so you'ld have to first let the server know which color the bullet shall be and than also provide this information back to all players after you Network.Spawn them.
You don't need to keep updating the property block. Once is enough. You've also got to make sure that the material allows instancing, and that the shader supports instancing of "_Color". On the standard and sprite materials, you'll find that at the bottom of the inspector window for the material.
So, you can remove the test in Update, and modify the ColorSet method. In fact, on a Bullet type object, I'd be inclined to make it as lean as possible (ideally move all functionality to a central bullet manager, but that's off topic).
public void ColorSet(Color color)
{
renderer.GetPropertyBlock(matBlock);
matBlock.SetColor("_Color", color);
renderer.SetPropertyBlock(matBlock);
}
If this isn't helping, are you grabbing a bullet from a pool, which already has colours set on the objects? Is it simply a case that you're not setting the correct colour on already instantiated objects?
I'm also questioning why renderer.material didn't work (or if you ever tried it?). But, it's my understanding/guess that setting a new material actually breaks instancing. So you might actually get better performance from setting the property block anyway.
i've create a class having List as a property
here's the code
public class ScreenEventNTF
{
//property
public List<string> list_event_string { get; set; }
//constructor
public ScreenEventNTF(blah blah)
{
list_event_string = new List<string>();
}
// the not-working method
public void AddToStringTodraw(string string_inp)
{
list_event_string.Add(string_inp);
}
}
from the main class i do something like
ScreenEventNTF notifier = new ScreenEventNTF(blah blah);
notifier.AddToStringTodraw("Fabulous");
and i try to write all the string in notifier.list_event_string but it didn't seem to
have any value stored in the List
here's the code according to the coments.
foreach (string text in notifier.list_event_string)
spriteBatch.DrawString(font_test, text ,
vector_mouse, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
what am i doing wrong ??
please help me, any help will be appreciate.
The way you add to the list should work, and I think that it does work but the problem is that when you draw the string you either:
Draw in a location off-screen: check the value in vector_mouse
You're drawing white text. Is the background contrast enough?
You can say that the method isn't working if it is indeed called and there are no values present in the list. You can just confirm with the debugger, set a breakpoint just before the foreach (string...)
Also, if you're not using scale, rotation as I can see from your parameters, you can just use this overload of the DrawString method: DrawString(SpriteFont, string, Vector2, Color)
Finally, if the List field is public, why should you expose the add functionality in a separate method? That's not necessary unless there are going to be changes in encapsulation in the future.
Exactly how are you iterating through the list? Need to see the code that prints the elements in the List object.
I know this exact question has been asked, but the solution posted there doesn't seem to work for me. Here's the code I'm trying:
namespace ConsoleApplication5
{
class Program
{
enum Tile { Empty, White, Black };
using Board = Tile[8,8];
And the error I get:
Invalid token 'using' in class, struct, or interface member declaration
It seems the "using" clause must be moved outside the Program class, but my Tile enum doesn't exist there. So how am I supposed to do this?
It looks like you're trying to use a name to represent a specific way of instantiating a Tile[,] array.
Why not just declare a method that does this?
Tile[,] GetBoard()
{
return new Tile[8, 8];
}
Another option, though I'd consider this a little bit bizarre (not to mention hacky), would be to define a Board type with an implicit operator to convert to Tile[,], as follows:
public class Board
{
private Tile[,] tiles = new Tile[8, 8];
public static implicit operator Tile[,](Board board)
{
return board.tiles;
}
}
This would actually allow you to do this:
Tile[,] board = new Board();
You cannot use using like that.
You can only use for concrete types, not for 'constructors' as you have used.
Unfortunately, you cannot use using to declare a name for an array type. I don’t know why, but the C# specification doesn’t allow it.
However, you can get pretty close by simply declaring Board as a new type containing the array you want, for example:
public class Board
{
public Tile[,] Tiles = new Tile[8,8];
}
Now every time you say new Board(), you automatically get an 8x8 array of tiles.