- Home /
Problem while instantiating a gameObject through C# script at runtime
I am working on a scene in which there are some windows of building and some targets hidden.Randomly windows are selected to set to open/close , when window is open randomly different targets spawn at that point. I am almost done with this part. But I have a problem in instantiating a prefab (GameObjects of target) through script, Like there is an example of my script
Random_Enemies.cs:
if(window is down) // pseudo code
Instantiate(Enemy_Arr[(Random.Range(minE,maxE))],spawn1.transform.position,Quaternion.AngleAxis(180, Vector3.down));
Damager-receiver.js:
if(got hit)// pseudo code
Destroy(gameObject);
Instantiate(Coin, transform.position,transform.rotation);
Random_Enemies is a C# script that is spawning enemies at random when window is down, and Damage-Receiver scripts check if Ray cast hit the "target" tag, and instantiate a Coin in replacement to that object. But its not working ? Coin doesn't appear what I am missing , is it not possible to instantiate a prefab at runtime and then again instantiate another prefab at that position. Any help would be appreciated ?
Here are the script files, Enemies_Spawn.cs is spawning enemy at random on specified position , its working Fine. Then DamageReceiver.js file is calling another prefab in replacement of the enemy. And here is the problem it doesnt instantiate at that position. Why is that so ?
Enemies_Spawn.cs:
IEnumerator Start()
{
while (true) {
Enemies_Spawn();
yield return new WaitForSeconds(Random.Range(minT, maxT)); // wait X times for opening next window
}
}
void Enemies_Spawn()
{
targetSelect = Random.Range(minE,maxE);
switch(targetSelect)
{ // switch start
case 1: // for gameobject window 1
{
#region
// spawn enemies here at the position of Spawn gameobjects
Instantiate(Enemy_Arr[(Random.Range(minE,maxE))],spawn1.transform.position,Quaternion.AngleAxis(180, Vector3.down));
}
#endregion
break;
DamageReciever.js
#pragma strict
var hitPoints = 5.0;
var detonationDelay = 0.0;
var Sparkles : Transform;
var Coin : Rigidbody;
public static var score: int = 0;
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0) {
score = score + 5;
Invoke("DelayedDetonate", detonationDelay);
}
}
function DelayedDetonate () {
BroadcastMessage ("Detonate");
}
function Detonate () {
// save position of current target
transform.position = new Vector3(gameObject.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z);
transform.rotation = gameObject.transform.rotation;
Shooting.Enemy_dead +=1;
// Destroy ourselves
Destroy(gameObject);
if (Sparkles)
{
Instantiate(Sparkles, transform.position,transform.rotation);
}
if (Coin)
{
Instantiate(Coin, transform.position,transform.rotation);
}
}
// We require the barrel to be a rigidbody, so that it can do nice physics
@script RequireComponent (Rigidbody
)
Yes it's possible to instantiate any number of things at the same position. Cant help more without the real code.
Al right ! Here's is another point when I just Drop that prefab into scene (hierarchy) its works perfectly fine. But When I call it from script it doesn't instantiate the Coin. Let me post script here
I've added your answer below to the question (as it wasn't a solution). You can always edit your question to add more details
Answer by whydoidoit · Jul 02, 2012 at 07:50 AM
It appears that the Coin prefab is not initialised. I'm also not sure what you are doing with the position and rotation in there, they appear to be setting it back to the same value....
function Detonate () {
Shooting.Enemy_dead +=1;
if (Sparkles)
{
Instantiate(Sparkles, transform.position,transform.rotation);
}
if (Coin)
{
Instantiate(Coin, transform.position,transform.rotation);
}
// Destroy ourselves
Destroy(gameObject);
}
I have tried that too , it doesn't work . I just figured out another problem the Enemy prefab has a DamageReceiver script attached to it, which is then taking "Coin" prefab as a variable through inspector, but at run time it goes off and the script actually doesn't find the prefab. I have tried many ways to solve it and again assign it through inspector but it again goes off in prefab How to fix that at runtime?
Oh right, you set the Coin Prefab on the script? Anyway just drag the enemy prefab into the scene, assign the Coin to the inspector and the click the Apply button to make sure the prefab is updated. Drag another one it to be sure.
Yes ! its like "var Coin: Rigidbody" and am dropping the prefab in the inspector . Its working fine If I drop enemy into scene, but doesnt work when calling from script.
So it must be something to do with the prefabs in the array I guess. Hmmm.
If you temporarily just made it instantiate a single enemy from a prefab rather than the array does it work?
I just deleted all prefabs from scene and added them again into scene , assigned all prefabs of coin again . And now it does appear when enemy is dead (as replacement). Thanks I guess it was with the assigning problem.
Answer by The_Magical_Kiwi · Jul 02, 2012 at 08:56 AM
// Destroy ourselves
Destroy(gameObject);
if (Sparkles)
{
Instantiate(Sparkles, transform.position,transform.rotation);
}
if (Coin)
{
Instantiate(Coin, transform.position,transform.rotation);
}
It looks like you are instantiating the coin after you have destroyed the object that is spawning it, you mind find it can either no longer spawn the coin because the script has been deleted along with the gameobject, or that it can't get the game objects position/rotation for the same reason.
I could be wrong of course, I have a feeling it would throw an exception if this was the problem.
That was my first thought too - turns out we were both wrong! (I changed my answer above when it didn't work, and it's ok because it wasnt DestroyImmediate). Turns out it was something misconfigured on the enemy prefabs.