I need help with function OnCollisionEnter. When i hit one target with the bullet this script don't do anything. This script is attatch on Player, if I attatch on bullet object, variable points reset every time I shot other bullet. Please help me guys. Code (CSharp): using UnityEngine; using System.Collections; public class points : MonoBehaviour { public int points; // Use this for initialization void Start () { points = 0; } // Update is called once per frame void Update () { MaxPontos (); } void OnCollisionEnter(Collision target) { if(target.gameObject.tag == "Enemy") { points = points + 1; print("" + points); } } void MaxPontos(){ if (points >= 15) { Application.LoadLevel("scorelevel"); } } void OnGUI(){ GUI.Box(new Rect(600,10,90,25 ), "Score : " + points); } }
Did you try the collision with the player and an object without rigidbody like a wall ? I think OnCollisionEnter not work with two dynamics object, use OnTriggerEnter instead.
When pass on target with player, increase 1 in variable points, but when hit with bullet nothing happens
What you may want to try is having the collision check on your bullet script & the points stored on the player or a game manager script/object. When the bullet hits the enemy access the points variable stored on the other script & update it. If the script above is in the bullet then you are updating the points value stored on each bullet, if it is in the player then it only updates when the player collides with the enemy. & OnTriggerEnter is probably better
OnCollision### and OnTrigger### are called when the colliders on the same gameobject (or children) as this script collide with something. So the if the above script is on the player, the player would need to collide with the enemy to score points. If you want to shoot the enemy with a bullet to score points, you will need to put the collision code on the bullet. Tracking the overall score isn't done on bullets, they just need to tell another "GameManager", "ScoreManager", "whateverYouWantToCallItScript" that they have collided and the single instance of this other script keeps track of the one score. also, don't use the old legacy GUI if you are starting out, use the new UI https://unity3d.com/learn/tutorials/topics/user-interface-ui
https://unity3d.com/learn/tutorials/topics/scripting live sessions 1, 2, 3 and 8... one of them will cover off getting scripts and gameobjects to work together I would think
It's a good idea to work through the tutorials, doing them as you go, & then testing yourself by either modifying the project with new stuff you think of or applying it to your own simple project. That way it reinforces what you learnt & gets you to think about how you use it in your own situation.
I have read some tutorials and questions on forum and i test this code bellow. I attach on Player and i recivied this error: Assets/Scripts/potnso.cs(17,49): error CS0120: An object reference is required to access non-static member `BULLET_ThermalDetonator.points' Code (CSharp): using UnityEngine; using System.Collections; public class potnso: MonoBehaviour { public int score; public int scorefinal; // Use this for initialization void Start () { } // Update is called once per frame void Update () { MaxPontos (); GameObject bullet_prefab = GameObject.Find("bullet_prefab"); BULLET_ThermalDetonator _BULLET_ThermalDetonator = bullet_prefab.GetComponent<BULLET_ThermalDetonator>(); score = BULLET_ThermalDetonator.points; if (score == 1) { scorefinal=scorefinal+1; } } void MaxPontos(){ if (scorefinal >= 15) { Application.LoadLevel("scorelevel"); } } }
is the points on the bullet prefab public? I would've made the bullet find the points script & increment the score that way. Currently every frame your potnso script is looking for a bullet prefab but if you have more than one bullet in the scene at once then I don't think it will find them all
Yes, points is public. On bullet script, every bullet destroy in 3 seconds. If i put this time in 1 second, maybe it doesn't have more than 1 bullet
step through from the beginning. Why should the manager/score script keep track of every bullet? Why can't each bullet find the manager/points script & then update the points when needed (not every bullet will hit the enemy)? From that I would have each bullet find the script/object that has the score stored & then update that value as needed.