I have a prefab (MyPrefab) which has the following C# script attached to it;
public class MyPrefab : MonoBehaviour {
int mA;
void Start () {
}
void Update () {
}
public void SetA(val) {
mA = val;
}
}
Another script is being used to generate MyPrefab game objects using instantiate;
MyPrefab test = Instantiate(Resources.Load(“MyPrefab”));
test.SetA(10);
The problem i have is that the code above fails as Instantiate returns an Object and cannot be cast to a ‘MyPrefab’. If i make test an Object type everything works fine and my object is instantiated. But i cannot call the SetA method.
I’m trying to instantiate a prefab like this, but I’m not sure how to reference the name/path? I want to do this programmatically, not by dragging a reference to a variable in the editor.
For instance, I want to instantiate a bush from the terrain assets pack. I tried Resources.Load(“Bush1”) and Resources.Load(“Terrain Assets/Bushes/Bush1”) but neither seemed to work. I know this is a total noob error. Sorry.
I couldn’t find an example of how to reference a nested asset in the docs or wiki, and searching the forums gave me tons of irrelevant results.