- Home /
Alternate method found.
In a script that checks an object's assigned number, how can I check the highest numbered object.
I have a script which checks an object's assigned number, when the player enter's it's trigger.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class PlayerStatistics : MonoBehaviour {
public float m_PlayerGroundLvl = 1; //What level of ground object is on.
GroundLvlAssign m_GroundLvlAssign; //GroundlvlNumberScript
public Collider2D m_CenterOfBalance;
void OnTriggerEnter2D (Collider2D ground)
{
m_PlayerGroundLvl = ground.gameObject.GetComponent<GroundLvlAssign>().SetGroundLvl;
}
}
The ground level script only has SetGroundLevel
I would like to know how this script can be adapted to check for the object trigger that has the highest number attached so that only the highest is assigned to the player.
Answer by tormentoarmagedoom · Feb 01, 2019 at 10:03 AM
Good day.
I will not give you the script, i iwll gove you the concept, and you need to adapt to your script.
float highesValue = 0;
GameObjet ObjectWithHighestValue;
foreach (GameObject ObjectSelected in ObjectsTocheckList)
{
if (ObjectSelected.NumberOfTheObject > highesValue )
{
highesValue = ObjectSelected.NumberOfTheObject;
ObjectWithHighestValue = ObjectSelected;
}
}
if (ObjectWithHighestValue == null) Debug.Log (There are no objects with values!);
So it finishes with the object with highest value stored in ObjectWithHighestValue variable Use the logic process to adapt it to your script!
Bye!
Answer by CobbledGames · Feb 02, 2019 at 08:36 PM
Huh, ultimately it was a far simpler case than I expected. All I needed to do was to change my OnTriggerEnter2D into OnTriggerStay2D. Well thanks anyways.
Follow this Question
Related Questions
2D Sidescroller Issues Please help. 1 Answer
Double jump 2D Platformer 1 Answer
Distribute terrain in zones 3 Answers
How to make a script for "jetpack" boost animation 1 Answer
2D Player Movement C# 3 Answers