0
\$\begingroup\$

I have attached the following script to a number of weapon prefabs:

public class WeaponScript : MonoBehaviour {

    public enum Direction {Right, Straight, Left};
    public Direction weaponDir;        

    void Start () {
        prepareDirection (weaponDir);
    }

    void prepareDirection(Direction dir)    {
        if (dir == Direction.Straight)  {
            transform.localEulerAngles = new Vector3 (0, 0, 0);

        }

        else if(dir == Direction.Left)  {
            transform.localEulerAngles = new Vector3 (0, 0, -30);
        }

        else if(dir == Direction.Right) {
            transform.localEulerAngles = new Vector3 (0, 0, 30);

        }
    }
}

I also added the following script to an empty game object SetupWeapon:

public class SetupWeaponsScript : MonoBehaviour {

    public GameObject[] weaponPrefabs; 
    public GameObject[] weapons;

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

        for (int i = 0; i < weaponPrefabs.Length; i++)
        {
            weapons[i] = Instantiate(weaponPrefabs[i]) as GameObject;
        }

    }
}

When I select my SetupWeapon object, I am able to populate the weaponPrefabs array with the weapon prefabs I want to use, but I can't directly alter their parameters in that same inspector view. I want to be able to override the direction of each weapon prefab in the list, rather than use the value that's stored on the prefab itself.

How can I create the weaponsSetupScript in such a way that I can set the direction enum for each weapon entry at the same time that I set select the prefab in the editor? I believe it might have something to do with inheritance, just not too sure how to go about it though.

\$\endgroup\$
  • \$\begingroup\$ It's not immediately clear what's not working about your present example. It looks like you should be able to select your various weapon prefab objects in the Project window, set their weaponDir in the Inspector for the WeaponScript class, and then they'll be instantiated with the value you've set. Is it that you want to override this direction after instantiating the prefabs? You can do this by storing and instantiating the prefabs as the WeaponScript type rather than GameObject, or by using GetComponent to access their WeaponScript on demand. Or is there some other issue here? \$\endgroup\$ – DMGregory Jun 15 '17 at 3:58
  • \$\begingroup\$ @DMGregory I want to select both the weapon prefab and it's enum value at the same time for each element of weaponPrefabs using the editor. Something like a tuple of the weapon prefab and the Direction enum. \$\endgroup\$ – OnlyCodeMatters Jun 15 '17 at 4:08
0
\$\begingroup\$

If you want a tuple - a type that contains your two pieces of data together in a package - then just define one. ;)

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<WeaponScript>(weaponPrefabs[i].weapon);
            weapons[i].weaponDir = weaponPrefabs[i].direction;
        }        
    }    
}

In the Inspector it looks like this, so you can populate the prefab and direction side-by-side:

Example of populating the weaponPrefabs array in the Inspector

\$\endgroup\$
  • \$\begingroup\$ this is awesome. I am trying to achieve something that I don't think warrants a new question. I attached the above code to a WeaponGroup empty game object. I realized that I need to be organizing the weapons into several groups to add other group functionality to the above script. So I made another game object WeaponGroups and made WeaponGroup a prefab. I am a bit confused as to how I'll get the editor properties from the above script (SetupWeaponsScript) for another script (SetupWeaponGroupsScript). Hence having a WeaponGroup property that follows the above. \$\endgroup\$ – OnlyCodeMatters Jun 15 '17 at 15:11
  • \$\begingroup\$ Generally I would like to know how I can transfer the properties above so that each WeaponGroup (is added to a newly created SetupWeaponGroupsScript which 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 visible through SetupWeaponGroupsScript. \$\endgroup\$ – OnlyCodeMatters Jun 15 '17 at 15:43
  • \$\begingroup\$ That sounds to me like a new question. Maybe include a mock-up of what you want to see in the inspector, to make sure it's unambiguous what the answer should achieve as an end goal. \$\endgroup\$ – DMGregory Jun 15 '17 at 16:06
  • \$\begingroup\$ Here is a link to the hierarchy I would to see in the inspector imgur.com/a/EBO34. As I mentioned above, I have attached the above script to a WeaponGroup GameObject and made a prefab. \$\endgroup\$ – OnlyCodeMatters Jun 15 '17 at 17:22
  • 1
    \$\begingroup\$ How can I use DirectedWeapon[][] to access all the other properties in another script, as in the image? \$\endgroup\$ – OnlyCodeMatters Jun 15 '17 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.