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 Alp-Giray-Savrum · Apr 26, 2015 at 05:09 PM · collisionarraytriggervariablestatic

Script effects all gameobjects.

Hello Unity Community,

I've been working on static variables and array methods. Therefor I wrote a code piece which Works nice with one collider but it has problems when i add two...

Our Block Object's script

 //Object's color variable.
 var objectColor : String;
 //Object's Unique ID.
 var objectID : String;
 
 //At first...
 function Start () {
     
     objectID = (RandomColorGenerator.selectedColor.ToString() + Spawning.spawnCount.ToString());
     
     objectColor = RandomColorGenerator.selectedColor;
     
     gameObject.GetComponent(SpriteRenderer).color = RandomColorGenerator.color;
     
     gameObject.name = objectID;
 
 }
 
 function OnTriggerEnter2D(rowCollider : Collider2D) {
 
     var blocksIdArray = rowCollider.gameObject.GetComponent(RowScript).blocksIdArray;
     var blocksColorArray = rowCollider.gameObject.GetComponent(RowScript).blocksColorArray;
     
     if(rowCollider.gameObject.tag == "Row") {
     
         
         rowCollider.gameObject.GetComponent(RowScript).blocksIdArray.Push(objectID);
         rowCollider.gameObject.GetComponent(RowScript).blocksColorArray.Push(objectColor);
     
     }
 
 }

and our row's array script

 //Variable for blocks ID array
 public static var blocksIdArray = Array();
 //Variable for observing.
 var blocksIdArrayInspector  : String[];
 
 //Variable for blocks color array
 public static var blocksColorArray = Array();
 //Variable for observing.
 var blocksColorArrayInspector  : String[];
 
 function Start() {
 
     blocksColorArray.Clear();
     blocksIdArray.Clear();
 
 }
 
 function Update() {
 
     blocksColorArrayInspector = blocksColorArray;
     blocksIdArrayInspector = blocksIdArray;
 
 }

now when i do this;

alt text

My Cyan block collides with collider 6, not 7 but, they have same script. Why this problem happens ? Does anyone have any idea ?

sd.png (193.1 kB)
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 youngapprentice · Apr 26, 2015 at 05:18 PM 0
Share

Try and fix up your code formatting. Also, I don't have time right now to read all of this, but you mentioned static variables. If you have a script affecting all objects, then if they all depend on that one static variable, when you change that variable it will change on all objects. This can cause some hairy situations.

avatar image Alp-Giray-Savrum · Apr 26, 2015 at 05:21 PM 0
Share

Well actually all rows uses same script. But i want to change only collided row's script variable. Isn't it possible ?

avatar image maccabbe · Apr 26, 2015 at 06:44 PM 0
Share

What are you referring to when you say collider 6 and collider 7?

avatar image Alp-Giray-Savrum · Apr 26, 2015 at 06:52 PM 0
Share

alt text

Theese colliders.

ekran-alıntısı.png (56.2 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Apr 26, 2015 at 07:09 PM

I think youngapprentice got it right about your use of static variables here.

 public static var blocksIdArray = Array();

This is defining your data structure for each row, but it's global, so every row defining that overwrites it for the other rows each time. Whichever row loads that data last is the only one storing data in that variable.

 rowCollider.gameObject.GetComponent(RowScript).blocksIdArray.Push(objectID);

Here, you're using said variable. You're loading up data in OnTriggerEnter for the region entered, but the data it calls doesn't vary per row.

You'll want to change back away from static variables in this situation, because you DON'T want them to be the same for everything.

Comment
Add comment · Show 3 · 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 Alp-Giray-Savrum · Apr 26, 2015 at 07:19 PM 0
Share

That's exactly what I want. But if static variables applies to every code piece, what should i use ?

avatar image Eno-Khaon · Apr 26, 2015 at 09:40 PM 1
Share

You'd simply need a non-static variable. When each row generates its properties, they can be stored in a public variable. You'll need to have a script variable and use GetComponent(RowScript) to access them, but that's simply the way to separate them into their own entities.

 function OnTriggerEnter2D(rowCollider : Collider2D) {

      var blocksIdScript = rowCollider.gameObject.GetComponent(RowScript);

      var blocksIdArray = blocksIdScript.blocksIdArray;
      var blocksColorArray = blocksIdScript.blocksColorArray;
      
      if(rowCollider.gameObject.tag == "Row") {
      
          
          blocksIdScript.blocksIdArray.Push(objectID);
          blocksIdScript.blocksColorArray.Push(objectColor);
      
      }
  
  }

You're already accessing the other script through the object rather than accessing the static variables globally, so you wouldn't even really need to change anything there. I would recommend loading the script into its own variable, however, so you're not re-requesting the script four times at a time.

avatar image Alp-Giray-Savrum · Apr 27, 2015 at 01:58 PM 0
Share

Well it seems i need to write another algorithm... Thanks by the way.

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

Collecting Array items in order 1 Answer

Specific enemy variable affects all enemies 2 Answers

Command all array variable values 1 Answer

Global Variables Refuse to Cooperate 1 Answer

Error(non static member): using public variable from other script JS 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