i`m trying to set a custom parameters to my GameObjects and access these parameters of one object by clicking on another object.
So, i have this useless code that will help me figure out what`s going on.
BaseScript.cs:
using UnityEngine;
using System.Collections;
public class BaseScript : MonoBehaviour {
public GameObject[] Bricks;
public GameObject BrickPrefab;
void Start () {
Bricks = new GameObject[5];
for (int i = 0; i <= 4; i++)
{
Bricks _= GameObject.Instantiate(BrickPrefab, new Vector3 (BrickPrefab.transform.position.x, i*1.1f, 0), BrickPrefab.transform.rotation) as GameObject;_
_ Bricks*.GetComponent().setCustomParameter(“EXAMPLE” + i.ToString());_
_ }_
_ }*_
* void Update () {*
* }*
* public void myMethod() {*
* Debug.Log (Bricks[0].GetComponent ().getCustomParameter ());*
* }*
}
and BrickScript.cs:
using UnityEngine;
using System.Collections;
public class BrickScript : MonoBehaviour {
* private string CustomParameter;*
* public BaseScript baseScript;*
* void Start () {*
* }*
* void Update () {*
* }*
* public void setCustomParameter(string CustomParameter) {*
* this.CustomParameter = CustomParameter;*
* }*
* public string getCustomParameter() {*
* return CustomParameter;*
* }*
* void OnMouseDown() {*
* baseScript.myMethod ();*
* }*
}
So, when i run the game it instantiates a five cubes “Bricks” out of my BrickPrefab. Each Brick has its own CustomParameter (EXAMPLE1, EXAMPLE2…EXAMPLE5) . I can see in the Inspector that these parameters are set properly if i make CustomParameter field public instead of private.
But, when i clicking on each Brick, i expecting “EXAMPLE0” in the console, but it throws me: "*IndexOutOfRangeException: Array index is out of range.
BaseScript.myMethod () (at Assets/BaseScript.cs:24)
BrickScript.OnMouseDown () (at Assets/BrickScript.cs:26)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)*"
Here is a screenshot:
[27601-untitled.png|27601]_
Im new to Unity3d and i guess it should be somethink simple. But i just dont understand how to assign everything properly… May be there is a way to assign things programmatically? So please tell me what i did wrong… Thank you!
_