- Home /
View script variables within an array of scripts in Inspector
Problem: My problem is an interconnected two parter, fist I need to be able to edit variables of a script within an array in the inspector. Second I need to be able to call said scripts functions from the inspector.
Solutions I know of:
problem 1:
Serialize the script and remove the MonoBehaviour from the script to reveal it's variables in the inspector.
don't use an array of scripts and manually add all instances of the script where needed.
problem 2:
Use sendMessage("functionName", value) or invoke("functionName", value).
hard code the use of each function for all possible uses.
You may already see the conflict. both sendMessage and invoke are part of MonoBehaviour, I remove mono I can edit the scripts variables as needed, but I loose the ability to call it's functions.
Thus far I've spent two days looking for an answer with no results. I need the ability to edit the variables in the inspector AND I need some way of invoking the scripts functions from the inspector.
Scripts:
The scrip within the array:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class BaseAttribute : MonoBehaviour
{
public string name;
public float normal;
public float augmented;
public float cap;
public float current
public float max;
public float CalcResult()//one of the functions for clarity
{
current += -current + (normal + augmented);
Manager();
return current;
}
//other functions...
}
the script containing the array:
using UnityEngine;
using System.Collections;
public class CCM : MonoBehaviour
{
public BaseAttribute[] attributes;
void Awake()
{
for(int i = 0; i < attributes.Length; i++)
attributes[i] = new BaseAttribute() as BaseAttribute;//gameObject.AddComponent<BaseAttribute>() as BaseAttribute;
}
}
A Script Using the Container 'for full exposure'
A test script acting on the script array, added to the object sometime during runtime The reason for the 'for' and 'foreach' discrepancy, my custom dictionary class is not itterable and I'm not sure how/where to implement IEnumerator. The many examples I've seen where not very helpful. But that is not pertinent to this question.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Condition: MonoBehaviour {
private CCM target;
public string name;
public float lifeTime;
public float timeBorn;
public funcKeyValue[] effected;
[System.Serializable]
public class funcKeyValue
{
public string name;
public string[] key;
public int[] amount;
}
void Awake()
{
target = gameObject.GetComponent<CCM>() as CCM;
timeBorn = Time.time;
for(int i = 0; i < effected.Length; i++){
for(int k = 0; k < effected[i].key.Length; i++){
target.SendMessage(effected[i].key[k], effected[i].amount[k]);
}
}
}
void Update()
{
if(Time.time - timeBorn >= lifeTime)
{
foreach(funcKeyValue item in effected)
{
foreach(funcKeyValue subItem in item)
target.SendMessage(subItem.key, subItem.amount);
}
Destroy(this);
}
}
}
Answer by FortisVenaliter · Aug 20, 2015 at 09:26 PM
Sounds like you need a custom inspector. I suggest reading up on the Unity.Editor namespace, specifically the Editor class and CustomEditor attribute. You can derive from that and define exactly how your inspector should look for the parent class, including making an array of scripts openable for editing. The downside is that you have to define it all for yourself instead of using Unity's built in serialization inspector. It's not super-intuitive, but if you search for "Unity CustomEditor" you should be able to find a bunch of tutorials.