Monday, December 9, 2019

unity - How to access arrays of prefabs and other properties through the inspector in a given hierarchy


The script below is added to an empty game object WeaponGroup, which can be populated using the editor. I have made a new game object WeaponGroups which should have a script SetupWeaponGroupsScript. How can I transfer the properties below so that each WeaponGroup (SetupWeaponGroupsScript will have an array of WeaponGroup objects) is setup in a way similar to what is done below, so that I make the SetupWeaponsScript properties hidden to the inspector and populate them through SetupWeaponGroupsScript?



public class SetupWeaponsScript {

// Here's our tuple combining a weapon prefab and a direction.
[System.Serializable]
public struct DirectedWeapon {
public WeaponScript weapon;
public WeaponScript.Direction direction;
}

// The prefabs array is now this tuple type.

public DirectedWeapon[] weaponPrefabs;
public WeaponScript[] weapons;

void Start ()
{
weapons = new WeaponScript[weaponPrefabs.Length];

for (int i = 0; i < weaponPrefabs.Length; i++)
{
// Using typed Instantiate to save a GetComponent call.

weapons[i] = Instantiate(weaponPrefabs[i].weapon);
weapons[i].weaponDir = weaponPrefabs[i].direction;
}
}
}

In other words I'd like to have the following hierarchy in the editor:


enter image description here




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...