Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MiXer · Mar 06, 2013 at 06:47 PM · systemprogressachievement

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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:

Accessing Other Game Objects

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MiXer · Mar 07, 2013 at 07:48 PM 0
Share

Thanks it's working now.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges