C # script taking values from one object and modifying another.
Hi, I really new to Unity and scripting, I've never coded before and also never posted questions before because most of the time after hard research I would find answers on the web, but this is no exactly the case. Long story short, I started making a "game"(its actually an animated avatar) where you control a 3d humanoid through OSC(with the use of TOUCH OSC, wich is installed on my smartphone) I made it so I can control most of the movements I want(Head pose, eyes, mouth and neck) , now for controling expressions it becomes kinda difficult dealing with several controls controling each feature, so I wanted to make it so I just have to press one PUSH button from the touch OSC interface and it will send a signal to the different features(objects?(nose eyes, eyebrows)) I want to control, since the push sends a Boolean. I than convert it to float so I can use lerp to smoothen the transitions. The problem is that I want the different objects that recieve the signal from said PUSH BUTTON (1) to react differently and set a different parammeters to themselves(I wanw to chagne their Z Position value). I managed to send the signal (with a "master script that listens to the OSC messages sent by TOUCH OSC form the smartphone) to the different objects (each object has a kinda reciever JS script) and then the "master" script uses some VALUES that it takes from each of the object scripts to switch that unique signal form the push(1) to the different values set on the VALUES public list in each of the object scripts(I can set the values through inspector). Now since the VALUES list is a LIST, I used and IF/ELSE statement in the MASTER SCRIPT(cs) so that it only takes the values from the VALUES LIST i the objects script if this VALUES LIST COUNT is more than 0, and if it is 0 than just send the value without modification. THE BUG is that when one object has its VALUE LIST COUNT set to 0(through inspector) and another object(feature) has its VALUES LIST COUNT set to more than zero,when The inital value wich was sent from TOUCH OSC passes through the MASTER script, since one of them has its VALUE LIST COUNT 0, it should recieve only 1's, and the other one that has its VALUES LIST COUNT set to more than 0, it should have the value replaced by one of the VALUES LIST values from the object script... and it does exactly that, except that even though the first object's VALUES LIST COUNT is 0, its incoming value(push=1) is still changed with one of the values from the other object's VALUES LIST value, only when I delete this second object component(script), the first one starts recieving only 1's(as it should be since its VALUES LIST COUNT is 0). It's supposed to work like this. INCOMING signal = 1(button) goes to MASTER script, than it sends it to all the objects that have the reciever component/script modifying/not modifying it first depending on the objects VALUE LIST COUNT(if its 0, than the MASTER Script sends it as it is, but if it is more than 0, the MASTER scripts changes it with one of the values from the object's VALUES LIST) but the chagne its supposed to affect only the objects with the atached reciever scripts that have a LIST COUNT ofmore than 0. But the fist object with LIST COUNT = 0 also gets its incoming values changed by the values from the object that has LIST COUNT= more than 0. I dont know much programmation, I am mostly an artist so I searched for scripts to do the job and than took a DR.Frankenstein rol to piece the different things I needed into a functional UNITY project. Also English is not my native language.. so sorry for that. and I also dont code. I understand(kind of) coding and stuff. I started the project for visuals, and I tohugh I wouldnt have to code anything if I start with Unity(well I expected some tweks ther and there) but I started going deep(to my perspective) into the coding part, and I m getting lsot on this one. PLease HELP!
This is the MASTER script(c#) wich send all the values taken from another script wich listens to the OSC messages:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class UnityOSCListener : MonoBehaviour {
private GameObject target;
public bool debugMode;
public bool useAccelerometer = false;
public void OSCMessageReceived(OSC.NET.OSCMessage message){
string address = message.Address;
ArrayList args = message.Values;
if(debugMode) {
Debug.Log ("address : " + address + "values : " + args[0]);
}
//get the type of button
string type = address.Replace("/", string.Empty);
type = Regex.Replace(type, @"\d", string.Empty);
foreach(GameObject tmpObj in GameObject.FindGameObjectsWithTag("OSCTarget")){
//send data for simple target game objects
if(tmpObj.GetComponent<OSCSimpleTarget>()!= null) {
if(tmpObj.GetComponent<OSCSimpleTarget>().targetName == address) {
target = tmpObj;
target.SendMessage ("OnOscMessage", args);
}
}
//send data for multiple target game objects
if(tmpObj.GetComponent<OSCMultiTarget>()!= null) {
for(var i =0; i < tmpObj.GetComponent<OSCMultiTarget>().targetName.Count ;i++) {
//Here are the CONDITIONS for the modification(cnahge) of the oringinal value.
if(tmpObj.GetComponent<OSCMultiTarget>().targetName[i] == address) {
target = tmpObj;
tmpObj.GetComponent<OSCMultiTarget>().type = type;
if (tmpObj.GetComponent<OSCMultiTarget>().Values.Count > 0){
args[0] = tmpObj.GetComponent<OSCMultiTarget>().Values[i];
}
else if (tmpObj.GetComponent<OSCMultiTarget>().Values.Count == 0){
args = args;
}
target.SendMessage ("OnOscMessage", args);
}
}
}
//send data accelerometer
} // end foreach
}
}
Here is the RECIEVER scripts(JS) attached to the objects(facial features) to recieve the values from the MASTER script:
import System;
import System.Text.RegularExpressions;
import System.Collections.Generic;
var targetName : List.<String> = new List.<String>(); // declaration
public var type : String;
public var toggle :boolean = false;
public var push:boolean = false;
public var xyCoords : float[] = new float[2];
public var fader : float;
public var Values : List.<float> = new List.<float>(new float[(0)]); //This creates a LIST OF VALUES that the Master
//script uses to modify the original signal.
public var multitoggle : float;
public var AccelerometerXYZ : float[] = new float[3];
function Start () {
}
function Update () {
}
function OnOscMessage (args:ArrayList ) {
if(type == "push" && args[0] == 1) {
push = true;
} else if(type == "toggle" && args[0] == 1) {
toggle = true;
} else if(type == "toggle" && args[0] == 0) {
push = false;
toggle = false;
} else if (type == "multitoggle"){
multitoggle = args[0];
Debug.Log ("type : " + type + "values : " + args[0]);
}
//fader value
if(type == "rotary" || type == "fader" || type == "multifader" || type == "encoder") {
fader = args[0];
}
//type = xy
if(type =="xy") {
xyCoords[0] = args[0];
xyCoords[1] = args[1];
}
//accelerometer
if(type =="accxyz") {
AccelerometerXYZ[0] = args[0];
AccelerometerXYZ[1] = args[1];
AccelerometerXYZ[2] = args[2];
}
}
Thank you fro everything!
Good day.
If want aomeone to help you, dont make this kilometric posts... $$anonymous$$ake simple questions, i thinl nobody will read this long long post...
Sorry I wanted to make the problem clear.... I tihnk it is kinda simple... I just wanted to explain why I thin kits simple.. sorry I edit!
Your answer
Follow this Question
Related Questions
How to make a player move up then forward using ontrigger enter collider? 0 Answers
Top Down Game - Camera Follow Player and General Scripting Question 0 Answers
Why can't I move player's game piece on 2nd turn? 0 Answers
Unity doesn't recognize Java version on Mac 0 Answers
OnCollisonEnter2D Not Firing after checking collider 1 Answer