- Home /
On Collide add progress help
Hi Everyone! I have a problem with progress achievement system. using System.Linq; using UnityEngine; using System.Collections;
[System.Serializable]
public class Achievement
{
public string Name;
public string Description;
public Texture2D IconIncomplete;
public Texture2D IconComplete;
public int RewardPoints;
public float TargetProgress;
public bool Secret;
[HideInInspector]
public bool Earned = false;
private float currentProgress = 0.0f;
public bool AddProgress(float progress)
{
if (Earned)
{
return false;
}
currentProgress += progress;
if (currentProgress >= TargetProgress)
{
Earned = true;
return true;
}
return false;
}
public bool SetProgress(float progress)
{
if (Earned)
{
return false;
}
currentProgress = progress;
if (progress >= TargetProgress)
{
Earned = true;
return true;
}
return false;
}
public void OnGUI(Rect position, GUIStyle GUIStyleAchievementEarned, GUIStyle GUIStyleAchievementNotEarned)
{
if (Input.GetKey(KeyCode.A))
{
GUIStyle style = GUIStyleAchievementNotEarned;
if (Earned)
{
style = GUIStyleAchievementEarned;
}
GUI.BeginGroup(position);
GUI.Box(new Rect(0.0f, 0.0f, position.width, position.height), "");
if (Earned)
{
GUI.Box(new Rect(0.0f, 0.0f, position.height, position.height), IconComplete);
}
else
{
GUI.Box(new Rect(0.0f, 0.0f, position.height, position.height), IconIncomplete);
}
GUI.Label(new Rect(80.0f, 5.0f, position.width - 80.0f - 50.0f, 25.0f), Name, style);
if (Secret && !Earned)
{
GUI.Label(new Rect(80.0f, 25.0f, position.width - 80.0f, 25.0f), "Description Hidden!", style);
GUI.Label(new Rect(position.width - 50.0f, 5.0f, 25.0f, 25.0f), "???", style);
GUI.Label(new Rect(position.width - 250.0f, 50.0f, 250.0f, 25.0f), "Progress Hidden!", style);
}
else
{
GUI.Label(new Rect(80.0f, 25.0f, position.width - 80.0f, 25.0f), Description, style);
GUI.Label(new Rect(position.width - 50.0f, 5.0f, 25.0f, 25.0f), RewardPoints.ToString(), style);
GUI.Label(new Rect(position.width - 250.0f, 50.0f, 250.0f, 25.0f), "Progress: [" + currentProgress.ToString("0.#") + " out of " + TargetProgress.ToString("0.#") + "]", style);
}
GUI.EndGroup();
}
}
}
public class AchievementManager : MonoBehaviour
{
public Achievement[] Achievements;
public AudioClip EarnedSound;
public GUIStyle GUIStyleAchievementEarned;
public GUIStyle GUIStyleAchievementNotEarned;
private int currentRewardPoints = 0;
private int potentialRewardPoints = 0;
private Vector2 achievementScrollviewLocation = Vector2.zero;
void Start()
{
ValidateAchievements();
UpdateRewardPointTotals();
}
// Make sure the setup assumptions we have are met.
private void ValidateAchievements()
{
ArrayList usedNames = new ArrayList();
foreach (Achievement achievement in Achievements)
{
if (achievement.RewardPoints < 0)
{
Debug.LogError("AchievementManager::ValidateAchievements() - Achievement with negative RewardPoints! " + achievement.Name + " gives " + achievement.RewardPoints + " points!");
}
if (usedNames.Contains(achievement.Name))
{
Debug.LogError("AchievementManager::ValidateAchievements() - Duplicate achievement names! " + achievement.Name + " found more than once!");
}
usedNames.Add(achievement.Name);
}
}
private Achievement GetAchievementByName(string achievementName)
{
return Achievements.FirstOrDefault(achievement => achievement.Name == achievementName);
}
private void UpdateRewardPointTotals()
{
currentRewardPoints = 0;
potentialRewardPoints = 0;
foreach (Achievement achievement in Achievements)
{
if (achievement.Earned)
{
currentRewardPoints += achievement.RewardPoints;
}
potentialRewardPoints += achievement.RewardPoints;
}
}
public void AchievementEarned()
{
UpdateRewardPointTotals();
AudioSource.PlayClipAtPoint(EarnedSound, Camera.main.transform.position);
}
public void AddProgressToAchievement(string achievementName, float progressAmount)
{
Achievement achievement = GetAchievementByName(achievementName);
if (achievement == null)
{
Debug.LogWarning("AchievementManager::AddProgressToAchievement() - Trying to add progress to an achievemnet that doesn't exist: " + achievementName);
return;
}
if (achievement.AddProgress(progressAmount))
{
AchievementEarned();
}
}
public void SetProgressToAchievement(string achievementName, float newProgress)
{
Achievement achievement = GetAchievementByName(achievementName);
if (achievement == null)
{
Debug.LogWarning("AchievementManager::SetProgressToAchievement() - Trying to add progress to an achievemnet that doesn't exist: " + achievementName);
return;
}
if (achievement.SetProgress(newProgress))
{
AchievementEarned();
}
}
void OnGUI()
{
if (Input.GetKey(KeyCode.A))
{
float yValue = 5.0f;
float achievementGUIWidth = 500.0f;
GUI.Label(new Rect(200.0f, 5.0f, 200.0f, 25.0f), "CELE");
// Setup a scrollview, and then fill it with each achievement in our list.
achievementScrollviewLocation = GUI.BeginScrollView(new Rect(0.0f, 25.0f, achievementGUIWidth + 25.0f, 400.0f), achievementScrollviewLocation,
new Rect(0.0f, 0.0f, achievementGUIWidth, Achievements.Count() * 80.0f));
foreach (Achievement achievement in Achievements)
{
Rect position = new Rect(5.0f, yValue, achievementGUIWidth, 75.0f);
achievement.OnGUI(position, GUIStyleAchievementEarned, GUIStyleAchievementNotEarned);
yValue += 80.0f;
}
GUI.EndScrollView();
GUI.Label(new Rect(10.0f, 440.0f, 200.0f, 25.0f), "Reward Points: [" + currentRewardPoints + " out of " + potentialRewardPoints + "]");
}
}
}
and a script, who add a progress if collide with mesh
using System.Linq;
using UnityEngine;
using System.Collections;
public class mesh : MonoBehaviour {
void OnCollisionEnter(Collision collision) {
foreach (ContactPoint contact in collision.contacts) {
AchievementManager.AddProgressToAchievement("Ucieknij przed człowiekiem!", 5.0f);
}
}
}
when i compile the script script called mesh, unity wrote in debug Assets/mesh.cs(8,31): error CS0120: An object reference is required to access non-static member `AchievementManager.AddProgressToAchievement(string, float)' Can anyone help me with this code?
Answer by robertbu · Mar 06, 2013 at 10:00 PM
AchievementManager is derived from Monobehaviour, so you will be creating an object of type AchievementManager by attaching it to some game object. So what you need in order to call this methods is the to use the object, not the class. You can get access to the object in a variety of ways. The easiest would be to declare a variable at the top of your 'mesh' class like this:
public AchievementManager am;
Select the object that has the 'mesh' componenet in the hierarchy and then in the Inspector drag the object that has the AchievmentManager attached to the am variable. Then your code would use the object like:
am.AddProgressToAchievement("Ucieknij przed człowiekiem!", 5.0f);
There are other ways of connecting the object to the 'am' variable. See:
Your answer
![](https://koobas.hobune.stream/wayback/20220613093937im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Accessing local system ( File Browser ) 2 Answers
C# help in walking and jumping 1 Answer
Creating a Quest system 2 Answers
inventory system issues 0 Answers
why do i get 0 2 Answers