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 Cal2 · Nov 18, 2014 at 12:42 AM · instantiateprefabbeginnernull

prefab is null when I try to instantiate from another script

I have a script which instantiates a ground plane and puts random objs on it, in a somewhat random location. The prefabs are attached to a public variable and dragged and dropped on in the inspector. They work in the first script but when I try to call this method from another script I get "The prefab you want to instantiate is Null".

searching around I have found every answer to the "prefab null" to be load.resources(). And this does work, but it doesn't help me if I want to be able to switch around prefabs or learn what I am doing wrong in trying to call the other script (and it seems to be possible according to the other posts, just no explanation).

the error is on line 26 in RandObjs (the first code), though I think its the way I'm calling the method in the 2nd code at line 20.

any help would be greatly appreciated.

 public class RandObjs : MonoBehaviour {
     public GameObject[] obstacleArray;
     public GameObject buildingPF;
     
 
     
     // Use this for initialization
     void Start () {
         //GameObject building = Instantiate(buildingPF, new Vector3(13.5f, 0f, 3.5f), Quaternion.identity) as GameObject;
         //Build(3.5f, 16, 39);
         Build(28.5f, 16, 39);  // +25, for sizing reasons
         Build(53.5f, 41, 64);
         Build(78.5f, 66, 89);
     }
     
     public void Build (float buildingZ, int forIEquals, int forILimit){
         
         
         int objHorizontal1, objHorizontal2, objHorizontal3, x;
         float timeDelta = Time.deltaTime * 50;
         
         //each building and each verticle placemet ( i ) is 25units apart. prolly something +25 for a loop on creation to cut down code clutter.
         // also make it so that this code snippet can be called from buildingScr when a building is destroyed.
         
         //a return for the verticle positions
         GameObject building1 = Instantiate(buildingPF, new Vector3(13.5f, 0f, buildingZ), Quaternion.identity) as GameObject;
         building1.rigidbody.velocity = new Vector3(0, 0, -timeDelta);
     
         for (int i = forIEquals; i <= forILimit; i+=8){    
         
             objHorizontal1 = Random.Range(1,6);
             objHorizontal2 = Random.Range(1,6);
             objHorizontal3 = Random.Range(1,6);
         
             // if to check and not build ontop
             if ((objHorizontal1 != objHorizontal2) & (objHorizontal1 != objHorizontal3)){
                 //create 1    unless it is equal to others
                 print (objHorizontal1 +"11");
                 x = Random.Range(0,7);
                 GameObject obstacle1 = Instantiate(obstacleArray[x], new Vector3(objHorizontal1*4.5f, 0, i),  Quaternion.identity) as GameObject;
                 obstacle1.transform.parent = building1.transform;
             }
                 
             if (objHorizontal2 != objHorizontal3){
                 //create 2     unless it is = to 3
                 print (objHorizontal2 + "22");
                 x = Random.Range(0,7);
                 GameObject obstacle2 = Instantiate(obstacleArray[x], new Vector3(objHorizontal2*4.5f, 0, i),  Quaternion.identity) as GameObject;
                 obstacle2.transform.parent = building1.transform;
                 }
             // 3 is free to create
                 print (objHorizontal3 + "33");
                 x = Random.Range(0,7);
                 GameObject obstacle3 = Instantiate(obstacleArray[x], new Vector3(objHorizontal3*4.5f, 0, i),  Quaternion.identity) as GameObject;
                 obstacle3.transform.parent = building1.transform;
         }    
     }
 }    

my 2nd script.

 public class BuildingScr : MonoBehaviour {
 
 
     private Shredder3DScr shredder3d;
     private Controls player;
     public GameObject build;
     
     
     // Use this for initialization
     void OnCollisionEnter( Collision collision) {
         
         
         shredder3d = collision.gameObject.GetComponent<Shredder3DScr>();
         player = collision.gameObject.GetComponent<Controls>();
         
         if (shredder3d){
         Destroy(gameObject);
         
         //creates the building
         
             //RandObjs builder = GetComponent<RandObjs>();
             RandObjs builder = gameObject.AddComponent<RandObjs>();
             //builder.Build(78.5f, 66, 89); 
         
         
         }    
         
         if (player){
         print ("lost");
         }
     
     }
 
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 AlwaysSunny · Nov 18, 2014 at 12:23 AM 1
Share

Please share line numbers when sharing errors, or paste the entire error message. Proofreading 80+ lines without guidance is unpleasant. :)

1 Reply

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

Answer by Jeff-Kesselman · Nov 18, 2014 at 12:45 AM

You create a new component instance of type RandObjs in line 22 of your second script.

buildingPF will be null in that component because its is brand new and hasn't had it set.

Setting another instance of the component elsewhere has no effect on this. Every instance has its own buildingPF field with its own value.

Comment
Add comment · Show 2 · 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 Cal2 · Nov 18, 2014 at 03:46 AM 0
Share

That is exactly what I'm having trouble with. how do I call my method Build from the RandObjs script, in the BuildingScr script and not have buildingPF be null?

avatar image Cal2 · Nov 19, 2014 at 01:38 AM 0
Share

ok, I think I understand what your saying.

I replaced buildingPF with an argument so that each script would have its' own obj.

Thank you.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

the prefab you want to instantiate is null 1 Answer

Some Prefabs are not Loading 1 Answer

Initializing Prefab returns NullPointerException 1 Answer

ArgumentException: The prefab you want to instantiate is null. 1 Answer

How can I Instantiate a Sprite into my 2D Scene? 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