- Home /
Access a prefab from a child class
Hi everyone
I have this code
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Transform cell;
void Start () {
Instantiate(cell , new Vector3(30, 50 , 30) , Quaternion.identity);
Child child = new Child();
child.Create();
}
void Update () {
}
}
public class Child : Test {
public Child(){
}
public void Create(){
Instantiate(cell , new Vector3(30, 60 , 30) , Quaternion.identity);
}
}
My problem is that the first Instantiate works (it creates a cube). When I try however to access the "cell" prefab from the child class I get an error
"The prefab you want to instantiate is null". Is is not possible for me to access a parent's class prefab from a child class?
This worked however
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Transform cell;
void Start () {
Instantiate(cell , new Vector3(30, 50 , 30) , Quaternion.identity);
Child child = new Child();
child.Create();
}
void Update () {
}
}
public class Child : Test {
GameObject local = (GameObject)Instantiate(Resources.Load("cell"));
public Child(){
}
public void Create(){
Instantiate(local , new Vector3(30, 60 , 30) , Quaternion.identity);
}
}
So its not a matter of making it work or not, I just want to have a deeper understanding of unity's philosophy and why my first attempt didn't work...
Also a second question. How can Instantiate work with a Transform object? I thought Transform was used to store the position, rotation and scale of the object but not the object itself. So how can the instantiate function know what object to clone?
Thank you for your time
Did you set the value of the prefab on the script using the Inspector? If so then such variable settings are not inherited (it's kind of a short cut)
Answer by hvilela · Oct 29, 2012 at 09:30 PM
Your parent "cell" variable was set (by inspector, I guess), but the child's wasn't.
Try this:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Transform cell;
void Start () {
Instantiate(cell , new Vector3(30, 50 , 30) , Quaternion.identity);
Child child = new Child();
child.Create(cell);
}
void Update () {
}
}
public class Child : MonoBehaviour {
public Child(){
}
public void Create(Transform cell){
Instantiate(cell , new Vector3(30, 60 , 30) , Quaternion.identity);
}
}
Answer by Bunny83 · Oct 29, 2012 at 09:49 PM
You do almost everything wrong you could.
First You have to distinguish a class from an instance of a class. A class just describes how an instance should look like. The instance of a class, also called "an object" has the real variables which can hold a value. A subclass or child class is just derived from the parent class.
You can have one or multiple instances of the same class, but their variables are unique for each instance.
Just take for example a class Wallet. The class describes how the wallet looks like and how it works. The instance of this class (a real wallet) can contain money. Now think og another brand of wallets. This is our sub class. It's derived from the original brand. When you go into the wallet factory and take a new wallet, do you expect the money you put in the other wallet to be inside the new one?
The next big mistake is that you can't (or shouldn't) create a MonoBehaviour derived class with new. This will produce errors and the class might not work as you'd expect. A MonoBehaviour is a Component and the only way to create one is to use AddComponent of a GameObject.
In your case it looks like you want a "normal" class, so don't derive it from MonoBehaviour. I actually don't see what's the purpose of your child class. The point of deriving a class from another is to inherit it's variables and methods. It makes no sense to create a child class inside the parent class...
Again, a class is just the "blueprint" for an instance of this type.
Answer by tdgs · Oct 30, 2012 at 12:17 PM
You were both right and now I see what I did wrong. I thought that simply doing drag'n'drop was the equivalent of doing something like that
public class Test : MonoBehaviour {
public Transform cell = cube_prefab;
}
So I thought the value would be passed to all instances of the classes, as to their children too.So basically when you set a variable through the GUI , Unity creates an instance of your class with the assigned value, to your Transform variable.
Thank you very much this clarified to me a few things on how Unity works.I know it was a dump thing to do , but I am just experimenting right now.
About my second question how can you instantiate an object just using its Transform value , how is it possible? I thought Transform was used to store the position, rotation and scale of the object but not the object itself.
Again thank you for your help and for your quick answers.