- Home /
How can I access inspector-set variables through a class?
Hello, I have a class with a constructor that is supposed to set the local "texture" variable to a Sprite of a public array, set on the inspector panel. The problem is that classes can not access outside variables. Is there a workaround for this problem?
var itemSprites : Sprite[]; //Set in the Inspector panel
public class Item {
var name : String;
var texture : Sprite;
function Item(n : String) {
name = n;
texture = itemSprites[0]; //set "texture" to a Sprite defined in the Inspector panel
}
}
Answer by MacDx · Nov 06, 2017 at 02:17 AM
Make the constructor ask for a Sprite, just like you are doing it with the string right now, or you could just set the texture field just after creating the item object. Allowing a class to access outside variables in their constructor like you suggest is not a good object oriented design, you would be tightly coupling your item class with an external implementation.
The problem is that classes can not access outside variables. Is there a workaround for this problem?
If you are set on doing it this way, you could always wrap up the itemSprites field inside a singleton and get your stuff that way in the constructor but I do not advice it at all, even mentioning it makes me feel dirty.
Your answer
Follow this Question
Related Questions
How can I access the variables of arrays with multiple data types? 1 Answer
Accessing another script from a class 3 Answers
Class Object Array? 1 Answer
C# Array of Child Objects 1 Answer