I get values from 2 Devices "only one" is active at the same time. Both Classes should have the field bpm: Code (CSharp): public HeartRateDisplay + FitnessEquipmentDisplay bpm; is there a way to do this in c#?
Seems like pretty basic inheritance with accessors to me. Code (csharp): public abstract class Device { public abstract float bpm { get; } } public class HeartRateDisplay : Device { public override float bpm { get { return 2.5f; } } } public class FitnessEquipmentDisplay : Device { public override float bpm { get { return 3.5f; } } } Device device = new HeartRateDisplay(); Debug.Log(device.bpm); device = new FitnessEquipmentDisplay(); Debug.Log(device.bpm); Should output: Code (csharp): 2.5 3.5
2 times device = new? I try this out, but i think i need to work on this, thanks for the tip: My Script so far is: Code (CSharp): using UnityEngine; using UnityEngine.UI; using System.Collections; public class DemoUIDisplay : MonoBehaviour { // public ProfileSessionManager profileSessionManager; public BicyclePowerSim bicycleSim; public PowerMeterDisplay powerMeter; public HeartRateDisplay bpm; // public FitnessEquipmentDisplay bpm2; we need bpm for it :) // public FitnessEquipmentDisplay bpm; // public SpeedDisplay speed2; public CadenceDisplay cadence2; public FitnessEquipmentDisplay fed; public RoundCounter roundCounter; public GameObject MiniMap; public GameObject SaveExit; public CircularBlur radialBlur; public Text envText; public Text riderText; public Text speedText; public Text stridesText; // public Text StrideCadenceText; // public Text StrideDistanceText; // public Text StrideSpeedText; public float minutes; //= Mathf.Floor(timer / 60); public float seconds; //= timer % 60; public float timer; private bool gamePaused = true; public float power; public int rounds; public float distance; public float ftp; public float bpms; //Heartrate // public Text km1Text; public float sec = 3f; public float climb; void Update() { //cumulatedDistance += ((transform.position - previous position).magnitude); float totalF = Mathf.Abs(bicycleSim.Fgravity) + Mathf.Abs(bicycleSim.FrollingResitance) + Mathf.Abs(bicycleSim.FaeroDrag); float fpercent = 0; float rpercent = 0; float apercent = 0; if (totalF != 0) { fpercent = Mathf.Abs(bicycleSim.Fgravity) / totalF * 100; rpercent = Mathf.Abs(bicycleSim.FrollingResitance) / totalF * 100; apercent = Mathf.Abs(bicycleSim.FaeroDrag) / totalF * 100; } // distance = powerMeter.distanceTravelled; //3.3f distance = bicycleSim.distanceTravelled / 1000; ftp = powerMeter.instantaneousPower / bicycleSim.weight; // ftp = powerMeter.instantaneousPower / ProfileSessionManager.curPlayerWeight; bpms = bpm.heartRate; rounds = GameObject.Find("Bicycle/target").GetComponent<RoundCounter>().count; //rounds = roundCounter.count + 1; climb = bicycleSim.totalClimb; //deactivate MiniMap in UI if (Input.GetKeyUp(KeyCode.M)) { MiniMap.SetActive(!MiniMap.activeSelf); } if (distance >= 1000) { // km1Text.text = "Reached 1000Meter!"; } if (bicycleSim.speed <= 10) { SaveExit.SetActive(true); } else { SaveExit.SetActive(false); } float minSpeed = 40; float maxSpeed = 100; if (bicycleSim.speed >= minSpeed) { radialBlur.strength = Mathf.InverseLerp(minSpeed, maxSpeed, bicycleSim.speed); radialBlur.strength = .1f + .9f * radialBlur.strength; } if (bicycleSim.speed >= minSpeed) { radialBlur.strength = 0.5f; } else { radialBlur.strength = 0f; } if (gamePaused) { power = powerMeter.instantaneousPower; if (power >= 20) { timer += Time.deltaTime; } } int minutes = Mathf.FloorToInt(timer / 60F); int seconds = Mathf.FloorToInt(timer - minutes * 60); string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds); envText.text = /*"\n" +*/ "Slope: " + bicycleSim.slopeGrade.ToString("F1") + " %" + "\n" // + "CadenceOnly: " + cadence2.cadence.ToString("F0") + "RPM" + "\n" // + "SpeedOnly: " + speed2.speed.ToString("F2") + "Km/h" + "\n" // + "FitnessDevice: " + fed.SetTrainerResistance.ToString("F0") + "\n" // Keine Ahnung!? How that works !? // + "Gravity Force: " + bicycleSim.Fgravity.ToString("F1") + " n" + " (" + fpercent.ToString("F0") + "%)" + "\n" // + "Roll Resist Force: " + bicycleSim.FrollingResitance.ToString("F1") + " n" + " (" + rpercent.ToString("F0") + "%)" + "\n" + "Aero Drag Force: " + bicycleSim.FaeroDrag.ToString("F1") + " n" + " (" + apercent.ToString("F0") + "%)" + "\n" +"Rounds: " + rounds.ToString("F0") + "\n"; // + "Brake Force: " + bicycleSim.Fbrake.ToString("F1") + " n" + "\n" speedText.text = "Speed: " + bicycleSim.speed.ToString("F1") + " km/h" + "\n" + "Power: " + powerMeter.instantaneousPower.ToString("F0") + " w" + "\n" + "HeartRate: " +bpm.heartRate.ToString("F0") + " BPM" + "\n" // + "HeartRate: " + bpm2.heartRate.ToString("F0") + " BPM" + "\n" + "Cadence: " + powerMeter.instantaneousCadence.ToString("F1") + " RPM" + "\n"; /* stridesText.text =*/ // + "Strides:" + GameObject.Find("StrideDisplay").GetComponent<StrideDisplay>().strides.ToString("F0") + "Steps" + "\n"; // StrideCadenceText; // StrideDistanceText; // StrideSpeedText; riderText.text = //"TotalWeight: " + bicycleSim.totalMass.ToString("F1") + " kg" + "\n" // + "Frontal area: " + bicycleSim.frontalArea + " m2" + "\n" // + "Drag coef: " + bicycleSim.dragCoef + "\n" // + "Rol resist coef: " + bicycleSim.coefficientOfRollingResistance + "\n" // + "Drive train effectiveness: " + bicycleSim.drivetrainEffectiveness + " %" + "\n" /*+*/ "Drive Distance: " + distance.ToString ("F2") + " Km" + "\n" // + "Time" + timerFormatted.ToString() + "S" +"\n"; + "Time: " + niceTime.ToString() + "\n" + "Climb: " + bicycleSim.totalClimb.ToString("F0") + " Meter" + "\n" + "W/Kg: " + ftp.ToString("F2") + "\n"; } }
It is impossible to have 2 different objects with the same identifier in the same class It also doesn't make any sense to, take this example: bpm.myValue Would you expect this myVale to return from the FitnessEquipmentDisplay object or the HeartRateDisplay object
If the PowerMeterDisplay active i want to grab Power and Cadence from this PowerMeterDisplay If the FitnessEquipmentDisplay active i want to grab Power and Cadence from the the FitnessEquipmentDisplay the same with HeartRateDisplay vs. FitnessEquipmentDisplay
The code provided in the first reply would work. But I might suggest device1 and device2 (or even better names) instead of new'ing device twice.
Sorry but i do not understand what to do I tried this, but will not work: Code (CSharp): public abstract class DemoUIDisplay : MonoBehaviour { public abstract float bpm { get; } // public ProfileSessionManager profileSessionManager; public BicyclePowerSim bicycleSim; public PowerMeterDisplay powerMeter; // public HeartRateDisplay bpm; public class HeartRateDisplay : DemoUIDisplay { public override float bpm { get { return bpm; } } } public class FitnessEquipmentDisplay : DemoUIDisplay { public override float bpm { get { return bpm; } } }
Use the first code provided, along with my suggestion. Also, where are you checking which device is active? Your code is a bit complex so make a simple example, based on the first code, so that we can better understand your issue.
I do not check if active or not: both scripts or more are active. See my screenshot. https://forum.unity.com/attachments/unbenannt-png.361549/ All device can deliver a value, they are physical devices. The Fitness Device can deliver power,cadence or the powerMeter can deliver power,cadence. do i need to deactivate a script that is not active and i can just write for an example: Code (CSharp): riderAnimator.speed = Mathf.Min(GameObject.Find("PowerMeterDisplay").GetComponent<PowerMeterDisplay>().instantaneousCadence / 50f, 2); riderAnimator.speed = Mathf.Min(GameObject.Find("FitnessEquipmentDisplay").GetComponent<FitnessEquipmentDisplay>().cadence / 50f, 2); so if PowerMeter active values will be used from there and otherwise FitnessEquipmentDisplay will be used!?
So this works for my power. Code (CSharp): powerInput = (GameObject.Find("PowerMeterDisplay").GetComponent<PowerMeterDisplay>().instantaneousPower) | (GameObject.Find("FitnessEquipmentDisplay").GetComponent<FitnessEquipmentDisplay>().instantaneousPower);