I tried to affect an other variable

I try to affect an other variable inside an other class when an object collide a specific spot.

I tried this line wich should work

if(collision.gameObject.name==“WallTest”)
{
PlayerPrefab.GetComponent().CharacterState=1;
Debug.Log (“whooooooooooooo”);
}

The debug show that there is a collision detection but the CharacterState does not change to 1, is there anything I do wrong?

Hey!

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.

Hope you understand what i mean and good luck :slight_smile:

edit: i wrote my original example using 2d.

This line:

collision.GetComponent<Charactermovement>().CharacterState = 1;

should probably be:

other.gameObject.GetComponent<Charactermovement>().CharacterState = 1;

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

Yes, so when you collide with Wall Test, this function should be called on your character:

public void OnTriggerEnter(Collider other)

The other variable is the collider of what you hit. thus my code above

other.gameObject.GetComponent<Charactermovement>().CharacterState = 1;

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

No, I have 3 actor in the scene, a walltest wich is a wall, my player and a projectile., When the projectile hit the wall, my player change state

That would have been very useful information in the original post :slight_smile:
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;
        }
}