- Home /
Playerprefs doesn't work oncollisionenter
I have no idea why its doesn't work, i am trying to save and update the cash amount every time the player touch the coin so at the end(at another script i can call):
GUI.Box(......... ""+PlayerPrefs.GetInt(cScript.key));
The script:
var amountOfCash : int;
var cScript : Store;
function OnCollisionEnter(c:Collision){
if(c.collider.tag == "Player" && gameObject.renderer.material.color.a != 0){
print("Call1");//Calls
amountOfCash = PlayerPrefs.GetInt(cScript.key);
amountOfCash+=10;
PlayerPrefs.SetInt(cScript.key, amountOfCash);
print("Call2");//not calls
Destroy(gameObject);
}
}
cScript is an object of type 'Store' class(contain key string)
gameObject.renderer.material.color.a != 0)
is because i made a fade effect for the texture so if alpha is equal to 0(On other script) the object is destroyed regardless to this script
the cScript.key is a string in class 'Store' and in order to call the GetInt function you need to put as a parameter a key.
Does the OnCollisionEnter method get called? Do you call the gui stuff in the same script as your collision method?
Yes the onCollisionEnter is called, i stated in code:
print("Call1");//Calls
but after the attempt to save its doesn't calls
print("Call2");//not calls
And no, the gui is in another script.
Edit: Found why its not working.. The nullreferecnce was giving me clue about an object which is not exist, i tried to get a component from this game object and the component was on the player object. so i simply used:
function Start(){
playerObj = GameObject.FindGameObjectWithTag("tag");
cScript = playerObj.GetComponent("Store");//Here was the problem..
}
and then simply use the cScript as it should work.
Your answer
Follow this Question
Related Questions
Playerprefs not storing data? 0 Answers
Does anyone know why continuously saving to playerprefs would cause a performance drop ? 0 Answers
Playerprefs resetting after quit 2 Answers
Is PlayerPrefs.Save() necessary ? (My game saves without it) 2 Answers
How to save gameobject values with respect to scene? 1 Answer