- Home /
Assigning an object as variable to another object which is a GameObject
I have a terrain with a grid which has been setup using a script, it has overall terrain class with subclass tile class which contains all variables for each tile. I want to assign an class object to each tile which can be used as reference and call functions. Following pseudo code for example:
public class tile{
int id
string name
int x
int y
.... etc...
public Crop assignedCrop;
}
The Crop is a super-class which has subclasses of Wheat and Corn. Wheat and corn both have few extra variables and functions. I thought it would be as simple as inside the inGameEditing script which handles all users input and clicking, I have a terrain object which used to reference each tile and then I would write following code:
tile.assignedCrop = new Wheat(Time.time);
Thinking just declaring a new wheat object using its constructor would create an object and assign it to that tile's assignCrop variable but I'm getting errors from Unity saying I can't use new Keyword being that its from Monobehaviour and have to use the AddComponent() method. But I don't want to create gameObjects to attach my crop scripts too just to use the scripts and assign objects to other gameObjets as variables. If Anyone knows why this doesn't work the way I thought it would it would be greatly appreciated.
$$anonymous$$ake Crop, Corn and Wheat not inherit from $$anonymous$$onoBehavior.
Doh, who'd have thought I'd miss something like that. Thanks for putting that out, no errors now!