• 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 /
  • Help Room /
avatar image
0
Question by ddiskyp · Sep 14, 2018 at 10:38 AM · scripting problemprefab-instance

How to create prefab instance and add it to the scene from script

I give up to try again after 3-4 hours and need some help. All what i want is create an object (prefab) on scene after player press Spacebar. So my code now:

 using UnityEngine;
 using System;
 
 public class PlayerController : MonoBehaviour
 {
     public GameObject missle;
     public PlayerController()
     {
         
     }
     void Update()
     {
         var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
         var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
 
         transform.Rotate(0, x, 0);
         transform.Translate(0, 0, z);
 
         if (Input.anyKeyDown)
         {
             Debug.Log(Input.inputString);
             if (Input.inputString == " ") {
                 missle = Instantiate(Resources.Load("Assets/Prefabs/Missle.prefab")) as GameObject;
             };
         }
     }
 }

And i keep getting error "The Object you want to instantiate is null."

My path to prefab: alt text

screenshot-6.png (1.8 kB)
Comment
Add comment
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

1 Reply

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

Answer by misher · Sep 14, 2018 at 12:14 PM

First. If you want to load the prefab using Resources.Load() your prefab should be in a folder called "Resources" or in a subfolder (like "Resorces/Prefabs"). You can also have multiple "Resources" folders in your project. Second, when loading asset with Resources.Load() you must specify only relative path to your asset in one of your resources folders, like Resources.Load("Prefabs/Missle");. Note that you don't need to put any file extension at the end (no .asset). Third, i can see you are trying to instantiate your prefab in MonoBehaviour script. You can directly reference your prefab using serialized field instead of loading it from resourced:

 [SerializeField] GameObjecct prefab; // reference
 ...
 var missle = Instantiate<GameObject>(prefab);

Fourth. If you want to do something when user press space bar:

 void Update () {
     if(Input.GetKeyDown(KeyCode.Space))
     {
         var missle = Instantiate<GameObject>(prefab);
     }
 }

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 ddiskyp · Sep 14, 2018 at 12:55 PM 0
Share

Im strict done your instructions: 1) Put [SerializedField] GameObject $$anonymous$$issle; outside methods but had no luck and got error: The type or namespace name SerializedField' could not be found`. And also i dont get what prefab in your example is, i thought here must be some path to him. 2) I moved my prefab to "Resorces" folder and tried again with my old code but with new path: missle = Instantiate(Resources.Load("$$anonymous$$issle")) as GameObject; result the same. Also i tried pathes "Resorces/$$anonymous$$issle" and "Assets/Resorces/$$anonymous$$issle.prefab" but no luck. And this code: var missle = Instantiate<GameObject>(Resources.Load("$$anonymous$$issle")); produce an error: "Argument #1' cannot convert UnityEngine.Object' expression to type `UnityEngine.GameObject'" Any other suggestions please?

avatar image ddiskyp · Sep 14, 2018 at 01:02 PM 0
Share

NV$$anonymous$$, it was just an typo in your answer about Resources folder an im double it. Finally it work, but only with my old code and just new path. If you can explain errors with your code im glad to read it. But anyway thanks.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

220 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

I want to draw prefab over another cloned prefab 1 Answer

Problem renaming prefab instance in scene (from custom editor window) 1 Answer

How to get scripts to detect foreign variable change? 0 Answers

Help I've Lost all Hope, A script stopped working on my Player Character Prefab! 0 Answers

How to move a specific object via C# 0 Answers


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