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 sebascontra · May 10, 2012 at 02:12 PM · prefabsobjectsif else

If /else statement can be used like this?

Hi everybody,

I'm trying to switch from a grid to a circle representation of a GameObject (prefab), but I cannot get the transition correctly, here is the code:

it should instantiates a prefab in a grid

     var prefab : GameObject;
     var gridX = 5;
     var gridY = 4;
     var spacing = 2.0;
     
     var numberOfObjects : int = 20;
     var radius = 2;
     var IsGrid : boolean = true;
     
     
     function Start () 
     {
      if(Input.GetButtonDown("Fire1"))
      IsGrid = !IsGrid;
      
      if(IsGrid == false)
      {
          for(var i = 0; i<numberOfObjects;i++){
              var angle = i * Mathf.PI * 2 / numberOfObjects;
              var pos = Vector3 (Mathf.Cos(angle),0,Mathf.Sin(angle))*radius;
              Instantiate(prefab,pos,Quaternion.identity);
          }
      }
      else
      {
          
     for (var y = 0; y < gridY; y++) {
     for (var x = 0; x < gridX; x++) {
     var pos2 = Vector3 (x, 0, y) * spacing;
     Instantiate(prefab, pos, Quaternion.identity);
     }
     }
     }
 
 }

and pressing "Fire1" key, should change to the circle, does it make sense? how can i do it?

thanks in advance for anyhelp...

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · May 10, 2012 at 02:22 PM

The 'Start' function only gets called once- in the first frame that the object exists. So, it only checks the fire button once- when it is first created. If you want a transition, you will need to do a few things.

1: Keep track of the currently existing objects. If you don't know what you've already created, you won't be able to delete them before creating the other pattern!

Simplest way to do this is to use one of the features of transform parenting. In your instantiation code, do this:

 var newObj : GameObject = Instantiate(prefab, pos, Quaternion.identity);
 newObj.transform.parent = transform;

This will make all the new object children of the controller.

Then, when you switch patterns, do this:

 for(var trans : Transform in transform)
 {
     Destroy(trans.gameObject);
 }

This will automatically delete all the children of the controller's transform!

Then, refactor it a bit. Put everything between about 'if(!isGrid)' and the end of the bit that spawns the grid into a new function:

 function ToggleGrid()
 {
     // put it here!
 }

Then to activate it, do this:

 function Update()
 {
     if(Input.GetButtonDown("Fire1"))
     {
         IsGrid = !IsGrid;
         ToggleGrid();
     }
 }

Remember to delete the previous pattern before instantiating the new one.

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 sebascontra · May 10, 2012 at 06:55 PM 0
Share

hey, thanks a lot, I'm very newbie scripting and don't know if i understood you very well, so let me paste the code again to get the chance of feedback, ok?

     var prefab : GameObject;
     var gridX = 5;
     var gridY = 4;
     var spacing = 2.0;
     var numberOfObjects : int = 20;
     var radius = 2;
     var IsGrid : boolean = true;
     var newObj : GameObject = Instantiate(prefab, pos, Quaternion.identity);
     newObj.transform.parent = transform;
 
 
     function Start () 
     {
      if(Input.GetButtonDown("Fire1"))
      IsGrid = !IsGrid;
     
     for(var trans : Transform in transform)
 {
     Destroy(trans.gameObject);
 }
     
     function ToggleGrid ()
     {
      if(IsGrid == false)
      {
         for(var i = 0; i<numberOfObjects;i++){
           var angle = i * $$anonymous$$athf.PI * 2 / numberOfObjects;
           var pos = Vector3 ($$anonymous$$athf.Cos(angle),0,$$anonymous$$athf.Sin(angle))*radius;
           Instantiate(prefab,pos,Quaternion.identity);
         }
      }
      else
      {
 
     for (var y = 0; y < gridY; y++) {
     for (var x = 0; x < gridX; x++) {
     var pos2 = Vector3 (x, 0, y) * spacing;
     Instantiate(prefab, pos, Quaternion.identity);
     }
     }
     }
     }
 
 }
 
 function Update()
 {
     if(Input.GetButtonDown("Fire1"))
     {
         IsGrid = !IsGrid;
         ToggleGrid();
     }
 }


bytheway i got this error: expecting (, found 'ToggleGrid'

thanks again,

avatar image syclamoth · May 11, 2012 at 03:41 AM 0
Share

You declared the function 'ToggleGrid' inside another function!!! You can't possibly expect that to work. You need to understand how functions work in c-like languages: in this case, anything inside the 'Start' function (the bits between the curly braces) will only run when the object gets created. You should remove that entirely, since it's only causing problems for you. You should add the bit for removing the existing grid into the beginning of the 'ToggleGrid' function- don't put it in Start, either.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy The Same Objects Before Instantiating 2 Answers

getting a list of objects from a data path 0 Answers

Instantiate sometimes creates more then 1 objects 1 Answer

Find Script from another script 2 Answers

setting variable in game object with instantiated object reference 2 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