Script Reference Help

Hello

I have an object “AchievementManager” with “AchievementManager” Script and a DontDestroyOnLoad Script from a previous Scene that i need to reference in my current scene.

Here is my code

using UnityEngine;
using System.Collections;

public class HealthColidePlayer : MonoBehaviour {
	
	GameObject AchievementManager1;
	GameObject AchievementManager2;
	
	
	void Start () {
		AchievementManager1 = GameObject.Find("AchievementManager");
        AchievementManager2 = AchievementManager1.GetComponent<AchievementManager>();
	}
	
	
void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
 		AchievementManager2.AddProgressToAchievement("Amped Up", 1.0f);
		//AchievementManager2.AddProgressToAchievement("Buzzing", 1.0f);
		//AchievementManager2.AddProgressToAchievement("AllSheHasGot", 1.0f);
        }

    }

}

Please someone explain to me the right way of doing it the right way.

P.S. I know this kind of question has been answered many time, but the answers dont seem to be helping.

Thanks

1 Answer

1

If I understood your question then you should change:

GameObject AchievementManager2;

with this:

AchievementManager AchievementManager2;

Also, be careful with DontDestroyOnLoad because you might end up with multiple similar objects that might interfere with your logics.

Well, it doesn't give any errors. but its still not working. As far as the duplication you mentioned, i bypassed that by preloading the object in my first scene, so no problems there.

Try getting directly into the script: AchievementManager1.GetComponent< AchievementManager >().AddProgressToAchievement("Amped Up", 1.0f); Also, try putting a print or debug.log to see if your OnCollisionEnter is being called as expected.

Had to go to work. But i will try it as soon as i get home and reply here

Well you really helped me man. Your last comment did the trick. Thank you very much