- Home /
how to set custom parameters to GameObjects and access them?
im 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[i] = GameObject.Instantiate(BrickPrefab, new Vector3 (BrickPrefab.transform.position.x, i*1.1f, 0), BrickPrefab.transform.rotation) as GameObject;
Bricks[i].GetComponent<BrickScript>().setCustomParameter("EXAMPLE" + i.ToString());
}
}
void Update () {
}
public void myMethod() {
Debug.Log (Bricks[0].GetComponent<BrickScript> ().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:
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!
I see nothing wrong with this script. I would suggest using statics for this type of manager class though.
Answer by Jeff-Kesselman · Jun 11, 2014 at 09:46 PM
You need to star debugging this.
Begin by printing the vale of Bricks[0] at the time access it.
If that throws an out of range exception then you need to figure out why its never getting initialized...
try putting a Debug right after you set Bricks in Start and see what it is there...
It didnt solve the problem. Just added this Debug.Log (Bricks[i].GetComponent ().getCustomParameter());
to that "for" loop where i instatntiating these Bricks. and the output is (EXA$$anonymous$$PLE0, EXA$$anonymous$$PLE1...EXA$$anonymous$$PLE5). So the problem is somewhere else.
Your answer
Follow this Question
Related Questions
How to assign GameObject to a instantiated prefab via Script(C#)? 2 Answers
Can't set the random position for an Instance. 1 Answer
The variable othertransform of Prefab has not been assigned 2 Answers
Check if object is destroyed on level load, if so instantiate prefab? 1 Answer
how to instantiate object with content prefab and not him self 2 Answers