How to create a variable in another object

In my game, I am creating many GameObjects in a 3 dimensional array like this:

internal var chunks : GameObject[,,]; 

Now I want to put a variable into each game object. For example:

chunks[1,0,3].newvariable : int = 4;

In this case, I want to create a variable called “newvariable” into that particular gameobject.

I also want to be able to access the variable later, like this:

print(chunks[1,0,3].newvariable); 

This code doesn’t work, so Im asking for your help to do this correctly. Can anyone help me accomplish this?

Unity js is not the same as browser js, as far as I know you can not assign dynamic properties in unity js. You can probably make some kind of dictionary and stick it in a component that all other components look up with GetComponent

sorry I use c# instead of js, but you could do this,

// on Awake in SomeScript.cs
foreach(GameObject gameObject in chuncks) {
   gameObject.AddComponent<SharedDictionary>();
}

// set in OneScript.cs
gameObject.GetComponent<SharedDictionary>().shared["newvariable"] = new Int(5); // or however you reference an int

// get in AnotherScript.cs
Int oldvariable = gameObject.GetComponent<SharedDictionary>().shared["newvariable"] as Int;

// SharedDictionary.cs
using UnityEngine;
using System.Collections.Generic;

class SharedDictionary : Component {
   public IDictionary<string, object> shared = new Dictionary<string, object>();
}