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 trigon · May 06, 2014 at 02:19 PM · c#instantiateinstantiating

Need help with instantiating a prefab in c#

Okay I can usually instantiate things just fine. Here is my code, please tell me what I'm doing wrong!

 public GameObject barricadePF; //assigned in the inspector
 int random = Random.Range (0, 100);
 
         if (random >= 25)
         {
             Debug.Log ("test");
             Vector3 barriPos1 = new Vector3 ((2*Random.Range(1, 4)), nextTrack.transform.position.y, (nextTrack.transform.position.z+2.25f));
 
             barricade1 = Instantiate (barricadePF) as GameObject;
 
             barricade1.transform.position = barriPos1;
             barricade1.transform.parent = nextTrack.transform;    
             barricade1.name = "barricade"+(Movement.trackNum);
         }
 
         if (random >= 75)
         {
             Debug.Log ("test2");
             Vector3 barriPos2 = new Vector3 ((2*Random.Range(1, 4)), nextTrack.transform.position.y, (nextTrack.transform.position.z+2.25f));
 
             barricade2 = Instantiate (barricadePF) as GameObject;
 
             barricade2.transform.position = barriPos2;
             barricade2.transform.parent = nextTrack.transform;    
             barricade2.name = "barricade"+(Movement.trackNum+1);
         }

This all happens when the player collides with an invisible gameobject (inside the OnCollisionExit function).

What this SHOULD do is instantiate the barricade if the random number is greater than or equal to 25, and instantiate it twice if it is greater than or equal to 75.

What it actually does is instantiate the barricade if the number is greater than or equal to 75. It does, however, output both Debug.Log messages. I'm so stuck here, both blocks are exactly the same except for the numbers.One of them works, the other doesn't.

UPDATE: After commenting out the whole second if block didn't work, my problem seems to be that the first instantiation isn't happening. Ever.

Comment
Add comment · Show 2
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 xortrox · May 06, 2014 at 02:23 PM 0
Share

The reason both debug messages are shown is because when your random >= 75 condition is met the random >= 25 condition is also true because 75 is already bigger than 25 making them both fire

avatar image trigon · May 06, 2014 at 02:25 PM 0
Share

I want them to both fire, but the first instantiate doesn't ever happen (and I want it to)

2 Replies

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

Answer by xortrox · May 06, 2014 at 02:49 PM

This code is all untested.

I would use a barricade handler with a list of barricade objects for easier control. (I don't know much about the game you're making so this may be entirely unecessary for your game type)

Example use:

 public class SomeMainObject : MonoBehaviour
 {
 
     BarricadeHandler barricadeHandler;
 
     void Start()
     {
 
         barricadeHandler = new BarricadeHandler();
         barricadeHandler.barricadePF = //Load prefab here
         barricadeHandler.nextTrack = //Pass nextTrack reference here
 
     }
 
     void OnCollisionEnter(Collider c)
     {
 
         //When your collision happens as before
         int random = Random.Range(0, 100);
         if(random >= 75)//Spawn 2 barricades 25% of the time
         {
 
             barricadeHandler.SpawnBarricade();
             barricadeHandler.SpawnBarricade();
 
         }
         else if(random < 75)//Spawn 1 barricade 75% of the time
         {
 
             barricadeHandler.SpawnBarricade();
 
         }
 
     }
 
 }

barricade handler script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;//For list
 
 public class BarricadeHandler
 {
     
     public GameObject barricadePF;
 
     List<GameObject> barricades = new List<GameObject>();
 
     public GameObject nextTrack;
 
     public void SpawnBarricade()
     {
 
         Debug.Log ("Spawning barricade");
         Vector3 barriPos = new Vector3 ((2*Random.Range(1, 4)), nextTrack.transform.position.y, (nextTrack.transform.position.z+2.25f));
  
          GameObject barricade = GameObject.Instantiate (barricadePF) as GameObject;
  
          barricade.transform.position = barriPos;
          barricade.transform.parent = nextTrack.transform;   
          barricade.name = "barricade"+(Movement.trackNum+1);
          barricades.Add(barricade);
          Debug.Log(string.Format("Barricade '{0}' spawned.", barricade.name));
 
     }
 
 }
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 trigon · May 06, 2014 at 03:11 PM 1
Share

this worked, thanks!

avatar image xortrox · May 06, 2014 at 03:33 PM 0
Share

No problem, please accept the answer so others will know it has been answered. :)

avatar image
0

Answer by YoungDeveloper · May 06, 2014 at 02:21 PM

Hi, so you have to run only one of these conditions. For second condition use

 else if (random >= 75){
 
 }

It will be launched only if first one was unsuccessful.

Comment
Add comment · Show 5 · 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 trigon · May 06, 2014 at 02:23 PM 0
Share

if that were true, it wouldn't output both Debug.Log statements. I tried it anyway, to no avail :(

avatar image YoungDeveloper · May 06, 2014 at 02:25 PM 0
Share

then just change them in places ? :p That's a silly solution but will work.

 if (random >= 75){
  
 }
 
 else if (random >= 25){
  
 }
avatar image YoungDeveloper · May 06, 2014 at 02:27 PM 0
Share

That is, by understanding what You want.

What this SHOULD do is instantiate the barricade if the random number is greater than or equal to 25, and instantiate it twice if it is greater than or equal to 75.

avatar image trigon · May 06, 2014 at 02:32 PM 0
Share

nope, didn't work. Outputs "test2" and instantiates when random >= 25, only outputs "test" when random >= 75, so the instantiation still isn't happening, and using the else does not make the other condition happen as well

avatar image YoungDeveloper · May 06, 2014 at 02:44 PM 0
Share

That's not how it's done anyway, you have a lot of unneeded code duplication there.

 public GameObject barricadePF; //assigned in the inspector
 
 private void Start(){
     Spawn();
 }
 
 private void Spawn(){
     int random = Random.Range (0, 100);
     int spawnAmount = 0;
 
     if (random >= 25) spawnAmount = 1;
     if (random >= 75) spawnAmount = 2;

     Debug.Log("Spawn "+spawnAmount+" barricades");
     if(spawnAmount != 0){
         for(int i = 0; i < spawnAmount; i++){
             Vector3 spawnPos = new Vector3 ((2*Random.Range(1, 4)), nextTrack.transform.position.y, (nextTrack.transform.position.z+2.25f));
 
             GameObject obj = Instantiate (barricadePF, spawnPos, Quaternion.identity) as GameObject; 
         }
     }
 }

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

21 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

Instantiating on Input 1 Answer

Instaniate Prefab 1 Answer

Multiple Cars not working 1 Answer

Instantiate ground/enemies 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