Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 elykl33t · Jun 29, 2011 at 03:42 PM · c#instantiateprefabcs0266

Instantiate object in world C#

I have been looking all over the Unity Forums and Unity Answers for the last two days trying to figure this out. I am trying to spawn a prefab, called cube1, into the world on the start of the scene. Thus far, I have not gotten it to work. My script is very simple.

 using UnityEngine;
 using System.Collections;
 
 public class RandomPlacement : MonoBehaviour {
 
     public GameObject myCube;
     public Vector3 spawnSpot = new Vector3(0,2,0);
     
     void Start() {
         GameObject cubeSpawn = (GameObject)Instantiate(myCube, new Vector3(0,2,0), transform.rotation);
     }
     
     void Update() {
     }
 }

I have dragged the prefab onto the "Default References" section for "My Cube" in the inspector on my script. I have also tried using

GameObject cubeSpawn = (GameObject)Instantiate(Resources.Load("Cube1"));

but that has not worked either. Can someone please help? Do I need to have the script and prefab in the same directory? (Under project, the script is in "Scripts" and the prefab is in "Resources").

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 elykl33t · Jun 29, 2011 at 03:46 PM 0
Share

I do get some errors or warnings having to do with GameObject and whatnot when I try a different method, and I always seem to get the warning "The Variable "cubeSpawn" is assigned but its value is never used". Does that make a difference?

Anyway, even when it does not error, the cube does not spawn.

3 Replies

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

Answer by Chris D · Jun 29, 2011 at 04:01 PM

I think the problem is that you're trying to instantiate the cube from itself in a scene where it does not yet exist.

If there isn't a GameObject already in the scene to which you can attach this script, nothing will be run. Start() runs when the object becomes active, but you need to have something there to call it.

Try this instead:

  1. place an empty gameobject in the scene

  2. attach your script to it

That should allow it to be called.

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 elykl33t · Jun 29, 2011 at 04:04 PM 0
Share

That worked! Thanks so much!

Does it apply for all scripts that you must have them attached for something in order to be run?

avatar image Chris D · Jun 29, 2011 at 04:09 PM 1
Share

Well, you have to have something running them in general. Even if it's just an empty gameObject (as in my answer), if code isn't attached to something in the scene or called from something in the scene, unity doesn't have a way of getting to it. Scripts aren't just run once they're created, they have to have specific instances.

I think there might be a way to tell a script to compile/run anyways, but I can't remember it off the top of my head. For now, just assume you have to run code from your scene : )

avatar image
-1

Answer by Ludiares.du · Jun 29, 2011 at 03:56 PM

Try changing this line:

 GameObject cubeSpawn = (GameObject)Instantiate(myCube, new Vector3(0,2,0), transform.rotation);

for:

 GameObject cubeSpawn = Instantiate(myCube, spawnSpot, Quaternion.identity);

or simply if you don't want to modify it anymore:

 Instantiate(myCube, spawnSpot, Quaternion.identity);
Comment
Add comment · Show 1 · 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 elykl33t · Jun 29, 2011 at 04:00 PM 0
Share

Simply returns error:

"Assets/Standard Assets/Scripts/RandomPlacement.cs(10,28): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)"

avatar image
0

Answer by cornflakers · Jan 16, 2012 at 07:45 PM

using UnityEngine; using System.Collections;

public class onLoad : MonoBehaviour {

 public GameObject Character;
 public Vector3 spawnLocation = new Vector3(0,2,0);
 
 // Use this for initialization
 void Start () {
     GameObject SpawnLocation = (GameObject)Instantiate(Character,spawnLocation,Quaternion.identity);
 }
 
 // Update is called once per frame
 void Update () {
 
 }

}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to instantiate multiple clones of the same prefab ? 1 Answer

How to randomly instantiate other prefabs parallel? 1 Answer

Destorying prefab and instanstiating again? 0 Answers

Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers

Accessing Script on instated prefab 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