Bonjour à toutes et à tous. J'espère que vous allez bien


J'ai un petit problème sur Unity3d dans mon code. Je fais un petit jeu auquel le joueur incarne un tank en 3d qui tire sur des tourelles qui suivent le joueur quand il rentre dans le Trigger. Dans le code qui détruit le joueur quand il est touché au bout de

3 tir d'une tourelle , il m'affiche un message d'erreur qui me dit que le "Transform' a été détruit mais vous essayez toujours d'y accéder". Je sais pas comment faire pour remédier à cela s'il vous plait. Si vous avez des idées pour m'aider je serais reconnaissant

Merci d'avance pour vos réponses.


Voici mes code:


Code du Third Personn Controler:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
 
public class ThirdPersonCamera : MonoBehaviour
{
    public bool lockCursor;
    public float mouseSensivity = 10;
    public Transform target;
    public float dstFromTarget = 2;
 
    public Vector2 pitchMinMax = new Vector2(-40, 85);
 
    public float rotationSmoothTime = 0.12f;
    Vector3 rotationSmoothVelocity;
    Vector3 currentRotation;
 
    float yaw;
    float pitch;
 
    void Start()
    {
        if (lockCursor)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }
    void LateUpdate()
    {
        yaw += Input.GetAxis ("Mouse X") * mouseSensivity;
        pitch -= Input.GetAxis ("Mouse Y") * mouseSensivity;
        pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
 
        currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
 
        transform.eulerAngles = currentRotation;
 
        transform.position = target.position - transform.forward * dstFromTarget;
    }
}



Code Target Player:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
 
public class TargetPlayer : MonoBehaviour{
 
    public GameObject player;
    public float speed = 0.6f;
 
 
    void Start() {
 
 
        player = GameObject.FindGameObjectWithTag("Player");
 
    }
 
 
    void Update(){
 
       Vector3 playerDir = new Vector3(player.transform.position.x,  0.4f, player.transform.position.z) -transform.position;
        float step = speed * Time.deltaTime;
        Vector3 newDir = Vector3.RotateTowards(transform.forward, playerDir, step, 0.0f);
        transform.rotation = Quaternion.LookRotation(newDir);
 
 
    }
}



Code du Spawn Projectile

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
 
public class SpawnProjectile : MonoBehaviour{
 
 
 
    public GameObject projectiles;
    public float delay = 5.0f;
 
    public GameObject ennemyParent;
    public EnnemyBehavior ennemyBehaviorScript;
 
 
 
 
    void Start() {
 
 
        ennemyParent = gameObject.transform.root.gameObject;
        ennemyBehaviorScript = ennemyParent.GetComponentInChildren<EnnemyBehavior>();
 
 
 
 
    }
 
 
    void Update(){
 
        delay = Mathf.Clamp01(delay);
 
 
 
        if (delay > 0.0f){
 
            delay -= Time.deltaTime;
 
        }
        if (delay < 0.01f) {
 
            delay = 1.0f;
 
        }
 
        if (ennemyBehaviorScript. attack && delay == 1.0f) {
 
            GameObject projectile_0 = Instantiate(projectiles, transform.position, transform.rotation);
            projectile_0.GetComponent<Rigidbody>().AddForce(projectile_0.transform.position.x, projectile_0.transform.position.y, projectile_0.transform.position.z, ForceMode.Impulse);
 
 
        }
 
 
    }
}




Code Ennemy Behavior:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
 
 
public class EnnemyBehavior : MonoBehaviour{
 
    public bool attack = false;
 
 
 
 
    void OnTriggerEnter(Collider other){
 
        if (other.gameObject.CompareTag ("Player")){
 
            attack = true;
 
 
 
        }
 
 
    }
 
 
    void OnTriggerExit(Collider other) {
 
        if (other.gameObject.CompareTag("Player")) {
 
            attack = false;
 
        }
 
 
    }
 
 
 
}