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 bridged · Jul 24, 2012 at 12:57 PM · gameobjectinstantiateobjectresources.load

Cannot instantiate gameobject

Hey,

Here is the Script:

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour {
  
  private GameObject newBug;
  private bool spawn1 = true;
 
  
  // Update is called once per frame
  void Update () 
  {
  if(spawn1)
  {
  spawnBug();
  spawn1 = false;
  }
  }
  
  void spawnBug()
  {
  newBug = Instantiate (Resources.Load ("bug1"));
  }
 }

And this is the error:

error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

But I did it like > here < I also tried a lot of different things like GameObject.Instante and typof(GameObject).

What am I doing wrong?

The prefab "bug1" is an empty GameObject with a cube and a cylinder in it.

Are there any tutorials or good sites where I can learn what things like "as Transform", "as rigidbody", "typeof(GameObject)" and other things that you can use to instantiate are good for? Something else than the unity3d documentations (cause they don't really explain they just say)

thx alot

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

3 Replies

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

Answer by AlucardJay · Jul 24, 2012 at 03:07 PM

here's how your script works :

the update runs every frame, but the first time it runs, it calls spawnBug() which is only trying to load the prefab, not telling it where to be instantiated.

This raises more questions. Are you trying to only spawn the Bug once at the very start? Or spawn a bug every time spawn1 is true (as this is private, can only happen once or if the same script changes it to true again).

If you do want to load the Bug from the resources folder, and not drop it in the Inspector for this script, then find the Bug and store a local reference to it at the Start :

 void Start() 
 {
     newBug = Resources.Load ("bug1");
 }

then in spawnBug() you want to correctly Instantiate the Bug : http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

 void spawnBug()
 {
     GameObject nextBug = Instantiate( newBug, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
 }

Vector3 is the position, Quaternion.identity roughly means a rotation of (0,0,0) . Try changing your script with these suggestions, then post your progress =]

oh, a couple of links like you asked, I'm not sure of your level but these are some good starter video's. follow these video tutorials, they are about 5mins each :

http://www.unity3dstudent.com/category/modules/essential-skills/

http://www.unity3dstudent.com/category/modules/beginner/

The video's are in reverse order, so scroll to the bottom and start at E00 , then on the second link B00

Hopefully this will introduce parts of using Unity as well as scripting. Have Fun =]

Also there is this great link to several starter tutorials : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html

Comment
Add comment · Show 3 · 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 AlucardJay · Jul 24, 2012 at 03:09 PM 0
Share

just a tip for the Unity Script References, when they give a script example, look above it to the right, you should see Javascript |/ , click on this, then select C#, the example should now be in C# for you =]

avatar image bridged · Jul 24, 2012 at 03:30 PM 0
Share

I just tried what you wrote, but if I make your first code

newBug = ...

and then

GameObject nextBug = ...

I get the same Object 2 times. I put both lines one after another in the spawnBug Function, because the script is not put on the bug itself, it's just a very shorten sample of what actually is happening.

avatar image AlucardJay · Jul 24, 2012 at 03:48 PM 0
Share

Sorry, my stuff-up! (that's what I get for copy/paste). I meant to only load the Bug and store it as a local reference in the Start function, have edited the answer =]

I only showed GameObject nextBug = to demonstrate you can Instantiate into a variable, then use the reference in that variable to manipulate the Bug after Instantiation.

avatar image
0

Answer by MihaPro · Jul 24, 2012 at 03:30 PM

Try:

 newBug = Instantiate (Resources.Load ("bug1")) as GameObject;

Instantiate return Object. Need type casting.

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 bridged · Jul 24, 2012 at 06:53 PM

It works when I do it like this:

 using UnityEngine;
 using System.Collections;
 
 public class brain1 : MonoBehaviour {
  
  public GameObject level;
  public Object nextContainer;
  public GameObject army;
  
  public int i = 1;
  
  // Use this for initialization
  void Start ()
  {
  
  }
  
  // Update is called once per frame
  void Update () {
  
  if(Input.GetKeyDown (KeyCode.U))
  {
  if(level)
  Destroy(level);
  
  nextContainer = Instantiate (Resources.Load ("container"+i));
  level = Instantiate (nextContainer, Vector3.zero, Quaternion.identity) as GameObject;
  Destroy (nextContainer);
  i++;
  
  army = GameObject.Find("army");
  
  Debug.Log (army.transform.childCount+" - "+i);
  }
  
  if(Input.GetKeyDown (KeyCode.Z))
  Destroy (level);
  
  
  }
 }


If you can explain me what sense it makes to Instantiate it twice and delete it once I would be happy. (I hope for now that this will fix the problem I have with all the other scripts and stuff)

Comment
Add comment · Show 4 · 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 AlucardJay · Jul 24, 2012 at 07:00 PM 0
Share

sry, but you missed my point (my fault 'cos i did have an Instantiate when I loaded in the first answer).

You want to store a reference to the object that is to be instantiated once only at the start, then use that reference in your Instantiate command.

If there are several 'containers' that need to be loaded, load them into an array at the Start

Then, if you instantiate into a variable, it creates the object, but you have a reference to it to change things later on.

avatar image AlucardJay · Jul 24, 2012 at 07:19 PM 0
Share

Something like this (UNTESTED) :

  using UnityEngine;
  using System.Collections;

  public class brain1 : $$anonymous$$onoBehaviour {

  public GameObject level;
  public GameObject[] nextContainer;
  public GameObject army;

  public int i = 1;

  // Use this for initialization
  void Start ()
  {
      nextContainer = new GameObject[4]; // set the number of resources to load HERE
 
      for ( var p : int = 0; p < nextContainer.length; p ++ )
      {
          nextContainer[p] = Resources.Load( "container" + (p + 1).ToString() );
      }
  }

  // Update is called once per frame
  void Update () {

      if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.U))
      {
          if(level)
          Destroy(level);

          level = Instantiate (nextContainer[i - 1], Vector3.zero, Quaternion.identity) as GameObject;
          i++;

          army = GameObject.Find("army");

          Debug.Log (army.transform.childCount + " - " + i);
      }

      if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Z))
          Destroy (level);


      }
  }
avatar image bridged · Jul 25, 2012 at 08:28 PM 0
Share

Thx!

I will take a closer look at this tomorrow. The Code I wrote above is wrong but I have a new finally really working code which I will post tomorrow too for the people who may have the same problem

avatar image AlucardJay · Jul 25, 2012 at 08:34 PM 0
Share

cool, sorry if I made it confusing. If you post your answer and it is correct/working, I'll give you a thumb up for effort and not giving up. Well Done =]

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

6 People are following this question.

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

Related Questions

object not spawning but no error message 2 Answers

how to instantiate object when I click 1 Answer

C# cast object to gameobject not working 2 Answers

How to instantiate within GameObject 1 Answer

Destroy the current GameObject? 7 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