Accessing a script attached to an object.

Hi all,

I have created a list of objects, which in this case are pegs of four different colours. When the pegs are instantiated a script called “pegObjectsClass” is added to the object and they are then added to the list. Here’s the code for instantiation :

static public void instantiatePegYellow () {
	pegObject = Instantiate(pegPrefabYellow, pegPosition, rotationAngle) as GameObject;
	pegObject.AddComponent("pegObjectsClass");
	pegObject.rigidbody.useGravity = false;
	cubeClass.pegObjects.Add(pegObject);
	Debug.Log(cubeClass.pegObjects.Count);
}

what I need to do is to access a bool that is held within the peg objects class called “isAttached” from the last peg that was added to the list.

I know that to access the last peg from the list I will use :

cubeClass.pegObjects.Count - 1;

What I need help with is being able to access the script attached to this specific peg, any help is greatly appreciated.

Regards

I think it should just be (off the top of my head):

cubeClass.pegObjects[cubeClass.pegObjects.Count - 1].pegObjectsClass.isAttached

though this might depend on what cubeClass.pegObjects is (an array or arraylist)

My mistake, it is a :

List<GameObject> pegObjects;

I believe this should work for a list:

cubeClass.pegObjects.Item[cubeClass.pegObjects.Count - 1].pegObjectsClass.isAttached

I believe it would be

cubeClass.pegObjects.Item[cubeClass.pegObjects.Count - 1].GetComponent(pegObjectClass).IsAttached

or you can do

cubeClass.pegObjects.Item[cubeClass.pegObjects.Count - 1].GetComponent("pegObjectClass").IsAttached

I get an error for the use of ‘Item’ :

Error CS1546 : Property ‘Item’ is not supported by the c# language. Try to call the accessor method ‘System.Collections.Generic.List<UnityEngine.GameObject>.get_Item(int)’ directly.

However I don’t know how I would go about doing this?

Does anyone have any knowledge on this above error who can help me?

I don’t know much about C# Lists, but maybe try this:

cubeClass.pegObjects[cubeClass.pegObjects.Count - 1].GetComponent(pegObjectClass).IsAttached

Im not having much luck with this problem. Is there any alternative to using a List in my specific case? The reason im using a List is that I needed to combine four different game objects with different tags.

What would be useful would be if I could add the objects to an array dyamically.

Is it possible to add the game objects to an array in the same way I added them to the list?

I guess the main problem is how to access a script attached to an object though!

What error does it give this time?

It’s messy, but if you only ever need to access the last peg you could just make a LastPeg variable and assign when your adding pegs to the list.

Yeah I do actually have a currPeg game object variable that I haven’t used so i’ll give that a try.

Thanks for all the help and advice!

Im finding it hard to access the script attached to the currPeg. I have used the documentation for unity to come up with this code so far:

public pegObjectsClass other;

other = currPeg.GetComponent("pegObjectsClass") as pegObjectsClass;

static public void instantiatePegYellow () {
		if(cubeClass.pegObjects.Count > 0) {
			if(other.isAttached == true) {
				pegObject = Instantiate(pegPrefabYellow, pegPosition, rotationAngle) as GameObject;
				pegObject.AddComponent("pegObjectsClass");
				pegObject.rigidbody.useGravity = false;
				cubeClass.pegObjects.Add(pegObject);
				Debug.Log(cubeClass.pegObjects.Count);
			} else {
				Destroy(cubeClass.pegObjects[cubeClass.pegObjects.Count - 1]);
				pegObject = Instantiate(pegPrefabYellow, pegPosition, rotationAngle) as GameObject;
				pegObject.AddComponent("pegObjectsClass");
				pegObject.rigidbody.useGravity = false;
				cubeClass.pegObjects.Add(pegObject);
				Debug.Log(cubeClass.pegObjects.Count);
			}
		} else {
			pegObject = Instantiate(pegPrefabYellow, pegPosition, rotationAngle) as GameObject;
			pegObject.AddComponent("pegObjectsClass");
			pegObject.rigidbody.useGravity = false;
			cubeClass.pegObjects.Add(pegObject);
			Debug.Log(cubeClass.pegObjects.Count);
		}
	}

however I get the error ‘An object reference is used to access the non-static member pegObject.other’ and if i make it static it still doesnt work, any ideas??