you need to “tell” the script what object to target. as shown in the code bellow :
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player") //checks if the collision belongs to the player
{
collision.GetComponent<Charactermovement>().CharacterState = 1;
Debug.Log("Woooooooooo");
}
when the collision happens, you check if the collision has tag that your player has (probably player) and if so you take collision and from it the charactermovement component as shown in the code snippet above. That way unity knows that the registered collision is from your player object and knows that it should have Charactermovement component.
Also just so you understand the reason your code wasn’t working is I assume PlayerPrefab is the Prefab object in your hierarchy? That variable is basically pointing to the master blueprint. Somewhere you instantiated a copy of PlayerPrefab, and thats the GameObject you need to get at to change your variable in it. (Which is what other.gameObject is… the reference to that copy you made).
I can try, but I dont want my character to change when something hit is. I want that my var characterState change when something hit something else and not the player hitself. Wall Test in this case
gets the gameObject of that collider, and then then the movement script on the Wall Test object and sets its character state to 1. That is assuming you have a Charactermovement script on Wall Test. I am assuming that from your original post
That would have been very useful information in the original post
So assuming that the projectile has the script that has the OnCollision, you need to make sure your player has a Tag, probably just “Player” then you can use this code:
// Add this code at top with your variables
GameObject player;
charactermovment charMoveScript;
// then in Start()
void Start()
{
player = GameObject.FindObjectWithTag("Player");
charMoveScript = player.GetComponent<charactermovement>();
// plus any other start code you had
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "WallTest")
{
charMoveScript.CharacterState=1;
}
}