Ok, here you go. I created a very simple project. Each Sphere script object(tagged as Sphere), when instantiated, starts a new instance of a very simple Csound instrument using sendScoreEvent()
. When I trigger the score event I pass the instance number as p5, and an initial random frequency as p5.
int index = GameObject.FindGameObjectsWithTag ("Sphere").Length;
gameObject.name = "Sphere"+index;
channelName = "freq"+index;
if(csoundUnity)
csoundUnity.sendScoreEvent("i\"SPHERE\" 0 10 " + Random.Range(100, 1000) + " " + index);
My Csound instrument creates a unique channel name based on this info:
instr SPHERE
aEnv linen .1, .01, p3, 0.01 ;amp env, low amp to avoid distortion..
iIndex = p5 ;index is passed as p5
SChannel sprintf "freq%d", iIndex ;construct unique channel name
kFreqScale chnget SChannel ;get freq info from Unity
a1 oscili aEnv, p4*kFreqScale ;set oscillator,
outs a1, a1 ;output signal
endin
In my Sphere’s update method I transform the frequency over time to drop from its initial pitch to close to 0, based on its current localScale, which as you see when you play the scene reduces in size over time.
csoundUnity.setChannel(channelName, transform.localScale.x/2f);
Not very interesting musically, but it shows how you can use a simple integer ID to communicate between each Csound instance, and each GameObject instance. Btw, you scene looks really cool. I won’t give away the details, but I look forward to seeing how it turns out! Here’s the project. I didn’t package Csound in order to keep the size down. You’ll need to add that yourself. You could probably just add this package to your existing one.
DynamicObjects.unitypackage (33.9 KB)
I recommend putting in these checks every time you call a CsoundUnity object:
if(csoundUnity)
csoundUnity.method()
You can remove them when it’s time to build the final game as they will slow things down somewhat. But in development I find they are really great for stopping Unity from crashing. Let me know how you get on.