- Home /
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
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
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 =]
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.
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.
Answer by MihaPro · Jul 24, 2012 at 03:30 PM
Try:
newBug = Instantiate (Resources.Load ("bug1")) as GameObject;
Instantiate return Object. Need type casting.
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)
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.
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);
}
}
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
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
![](https://koobas.hobune.stream/wayback/20220613074623im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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