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 AsemaKhan · Jul 02, 2012 at 06:29 AM · instantiateprefab

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

)

Comment
Add comment · Show 4
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 whydoidoit · Jul 02, 2012 at 07:14 AM 0
Share

Yes it's possible to instantiate any number of things at the same position. Cant help more without the real code.

avatar image AsemaKhan · Jul 02, 2012 at 07:26 AM 0
Share

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

avatar image whydoidoit · Jul 02, 2012 at 07:45 AM 0
Share

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

avatar image AsemaKhan · Jul 02, 2012 at 08:26 AM 0
Share

Alright Thank you !

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
 }

  
     
Comment
Add comment · Show 6 · 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 AsemaKhan · Jul 02, 2012 at 08:02 AM 0
Share

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?

avatar image whydoidoit · Jul 02, 2012 at 08:10 AM 0
Share

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.

avatar image AsemaKhan · Jul 02, 2012 at 08:27 AM 0
Share

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.

avatar image whydoidoit · Jul 02, 2012 at 08:35 AM 0
Share

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?

avatar image AsemaKhan · Jul 02, 2012 at 08:41 AM 0
Share

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.

Show more comments
avatar image
0

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.

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 whydoidoit · Jul 02, 2012 at 08:58 AM 1
Share

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.

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

Spawn game objects randomly with no overlapping 0 Answers

assign different values to prefabs on instantiation 2 Answers

setting the text of an instantiated prefab's child's guitext object 1 Answer

Get unity to recognize prefab in C# 2 Answers

Instantiated object rotation abruptly. 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