I’m new to C# but doing very well until I tried to create a group of objects that have public static variables, arrays and methods in them that I want accessed from another script.
The first part is my PuckContainerScript. I have 5 of these in my scene and each one controls 50 objects and variables. The script for it is below.
using UnityEngine;
using System.Collections;
public class PucksContainerScript : MonoBehaviour {
public static GameObject[] pucks;
public static Vector3[] puckpos;
public static float [] puckduration;
void Start()
{
var mypucks = GetComponentsInChildren<SpriteRenderer>();
int count = mypucks.GetLength(0);
pucks = new GameObject [count];
puckpos = new Vector3[count];
puckduration = new float[count];
count = 0;
foreach (SpriteRenderer sp in mypucks)
{
pucks[count] = sp.gameObject;
puckpos[count] = sp.transform.position;
puckduration[count] = 0f;
pucks[count].SetActive(false);
count++;
}
}
public static bool SetPuckPos(int who, float where)
{
bool retval = true;
if (pucks[who] != null)
{
puckpos[who].x = where;
pucks[who].transform.position = puckpos[who];
pucks[who].SetActive(true);
}
else retval = false;
return retval;
}
}
that part all works great! Where I am having trouble is another object with a script on it that I want to be able to access the PuckContainerScript from.
using UnityEngine;
using System.Collections;
public class TapPointsHandlerScript : MonoBehaviour {
public PucksContainerScript[] pucklists;
public bool randomizepositions;
void Update()
{
if (randomizepositions)
{
for (int clr = 0; clr < 5; clr++)
{
float mytime = 1f;
for (int index = 0; index < 49; index++)
{
// this is where I have trouble! I can't seem to figure out how to actually access
// the variables, arrays and methods in my array of scripts.
pucklists[clr].SetPuckPos(index, mytime);
pucklists[clr].SetActive(true);
pucklists[clr].puckduration[index] = Random.Range(0.1f, 2f);
mytime += Random.Range(1f, 5f);
}
}
randomizepositions = !randomizepositions;
}
}
}
I’ve tried all sorts of different combinations of how to access my PucksContainerScript but nothing really works. I get errors like:
Member ‘PucksContainerScript.SetPuckPos(int, float)’ cannot be accessed with an instance reference; qualify it with a type name instead
This has been driving me batty all weekend!