- Home /
script problem even though i have no errors
so if the rocket hit the player it should do damge but its not working here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class gidlmissle : MonoBehaviour
{
plazerhealth a;
public Transform player;
float speed = 1f;
public void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.Lerp(transform.position,player.position,speed*Time.deltaTime);
}
private void OnCollisionEnter(Collision collision)
{
if(collision.collider.tag=="player")
{
a = transform.GetComponent<plazerhealth>();
if(a != null)
{
a.takedamge(300);
}
}
}
}
Answer by tuinal · Jul 01, 2020 at 12:43 PM
You're calling transform.GetComponent (finding a component on the object the script is attached to), when you want to be calling collision.collider.GetComponent (finding a component on the object the collision involves).
Answer by tBurger · Jul 01, 2020 at 02:22 PM
What @tuinal said! transform.GetComponent() tries to find a component called "plazerhealth" on the same gameObject gridlmissile is attached to. So unless there actually is a component like that attached it will assign null to a.
On a different note: Please check out code readability principles, layouting and naming convention rules. You will do yourself and everyone else a favor by learning coding following some of those. Good luck :)
ok thanks but can you show me how to do it in code PLS. thanks all so for the second tip and i am sorry for the bad English its not my mother language.
Your answer
Follow this Question
Related Questions
Is it possible to raycast during edit mode? 1 Answer
Using Ray cast for click to move 2 Answers
Raycast Not Working properly 1 Answer
scripting problem 0 Answers
Can you figure out raycast origin position from RacyastHit? 2 Answers