Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tdgs · Oct 29, 2012 at 06:24 PM · instantiatetransformprefabs

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Oct 29, 2012 at 06:28 PM 0
Share

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)

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefab color is black 1 Answer

Instantiate Problem 2 Answers

The variable othertransform of Prefab has not been assigned 2 Answers

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

Trying to get a shoot function working in unity using instantiate and it isn't working. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges