This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I have a prefab called TileBehaviour which is used to create a grid of tiles. Originally each TileBehaviour accessed a singleton to get row and column info. I decided I wanted to make some kind of constructor. I called it "UnityConstructor." I've been discovering what works and doesn't work.
Here is the code that I use to create the TileBehaviours, after dragging in the prefab to a GameTracker object:
var tile = TileObj.GetComponent<TileBehaviour>();
tile.UnityConstruct( columnCount, rowCount,"Hello World" );
meanwhile, in TileBehaviour.cs, here are some fields and properties I tried experimenting with
//this must be public, if it's private, it won't be set
public string testString = "unset";
public string TestString { get => testString; private set => TestString = value; }
//this will not be changed by the UnityConstructor method, because it is a property. Rather, it won't be cloned by Instantiate.
public bool PseudoConstructRan { get; set; }
public object UnityConstruct(int column, int row, string testString )
{
//set Row, Column and Point
Row = row;
Column = column;
Point = new IntPoint2( Row, Column );
this.testString = testString;
TileId = GameState.Instance.TileCount ;
///some positioning code removed from here
PseudoConstructRan = true;
if( PseudoConstructRan )
{
print( "PseudoConstructRan" );
}
//Instantiate, store and return:
//Instantiate
var Instantiation = Instantiate( this );
if( Instantiation.PseudoConstructRan )
{
print( "Instantiation has desired qualities" );
}
else
{
Debug.LogWarning( "Intantiation does not have desired qualities!" );
print( $"Row set to {Row}, Instantiation.Row: {Instantiation.Row}" );
print( $"Test string set to {this.testString}, Instantiation.TestString: {Instantiation.TestString}" );
}
//store
TileCollection.Instance.AddTile( Instantiation );
//return
return Instantiation;
}
results: Instantiation.PseudoConstructRan is false (new set value is not cloned); in the console, I get these custom warnings:
Intantiation does not have desired qualities!
Row set to 40, Instantiation.Row: 40
Test string set to Hello World, Instantiation.TestString: Hello World
If I set testString to private, Instantiation.TestString remains "unset"
Anyway, my first thought on going through this is: I want to use custom constructors, but if I accidentally try to edit a property in one, it may be very hard to find the bug later. So, perhaps I should just not use properties. Writing properties is nice and all, but I'm used to just doing method calls from JavaScript anyway.
Anyway, I'm happy to hear any of your thoughts about how you made your pseudoconstructors. I was looking up about using the factory pattern in Unity, but that doesn't directly address this. Any info on nomenclature would also be great.
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/Unity2D/com...