Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by HTO15 · Aug 21, 2017 at 01:29 PM · instantiatetriggerscript.

How to get a public int equal the same number when added in all object that have the same script?

I'm trying to make a game where tiles get added to the floor continuously but the colour of the tiles are random when added. I've done this by Instantiating the selected tiles into the level when they collide into a trigger like this;

  private int rndnum;
    
     
     public int i = 2;
     private in u = 10;
     public GameObject Tile1;
     public GameObject Tile2;
     public GameObject Tile3;
     public GameObject Tile4;
     public GameObject Tile5;
    
 
     private void Start()
     {
       
     }
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             i = i++;
             print(i);
             {
                 rndnum = (Random.Range(1, 5));
 
                 if (rndnum == 1)
                 {
                     
                     Instantiate(Tile1, new Vector3(0, u + -11,i * 27.5f), Quaternion.Euler(new Vector3(20, 0, 0)));
                    
                    
                 }
                 if (rndnum == 2)
                 {
                     
                     Instantiate(Tile2, new Vector3(0, u + -11, i * 27.5f), Quaternion.Euler(new Vector3(20, 0, 0)));
 
                    
                 }
                 if (rndnum == 3)
                 {
                    
                     Instantiate(Tile3, new Vector3(0, u + -11, i * 27.5f), Quaternion.Euler(new Vector3(20, 0, 0)));
 
                    
                 }
                 if (rndnum == 4)
                 {
                    
                     Instantiate(Tile4, new Vector3(0,  u + -11, i * 27.5f), Quaternion.Euler(new Vector3(20, 0, 0)));
 
                    
                 }
                 if (rndnum == 5)
                 {
                     
                     Instantiate(Tile5, new Vector3(0,u + -11, i * 27.5f), Quaternion.Euler(new Vector3(20, 0, 0)));
 
                    
                 }
             }
         }
     }
 }

All of the box colliders have the same script but as the tiles are instantiated the 'i' value doesn't change between one trigger with its script and the next trigger so the tiles just spawn on top of each other.

Edit: Sorry should have mentioned this, the 'i' value is used so that the tiles spawn at a new vector right next to each other.

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
1
Best Answer

Answer by NoseKills · Aug 21, 2017 at 02:50 PM

Make the field static and it will belong to the class, not every instance of it separately.

 public static int tileCounter = 2;
 ...
 
 if (other.gameObject.CompareTag("Player"))
          {
              int i = tileCounter++;
              print(i);
 ...

(using the local variable i here is optional, you can just use tileCounter throughout the method)

Few other notes

  i = i++

this doesn't do anything because of the order in which things happen. It basically takes note of the value of x, increments x and then assigns to x the value it took note of before incrementing it.

  Random.Range(1, 5)

Random.Range with integers is exclusive of the second parameter. You will never get a 5 with this. With floats Random.Range is inclusive of both arguments.

 if (rndnum == 1)
 {                 }
 if (rndnum == 2)
 {                 }
 if (rndnum == 3)
 {                 }
 if (rndnum == 4)
 {                 }
 if (rndnum == 5)
 {                 }

If rndnum equals 1, there's no need to check if it equals 2, 3, 4 or 5. You should add else to the latter checks or use a switch-case.

 if (rndnum == 1)
 {                 }
 else if (rndnum == 2)
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 HTO15 · Aug 21, 2017 at 03:22 PM 0
Share

Thank you, this helped a lot but now there seems to be a problem with the 'u' value, I've tried to do the same thing as you did to the 'i' value by turning it into a static int but that doesn't seem to work either. Any help would be appreciated.

avatar image NoseKills HTO15 · Aug 22, 2017 at 07:17 AM 0
Share

I can't see you incrementing u anywhere. What is the problem you are getting ? An error ?

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

95 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Wanting audio to pause on trigger enter 1 Answer

instantiate keeps repeating itself 0 Answers

OnTriggerStay Doesn't work if not moving 2 Answers

is the Character Controller a Collider? 2 Answers

Stuttering in build 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