Problems with localScale

PLEASE THIS IS VERY URGENT!!!

I have two scripts. One is on the player and the other is on another object. Lets call that object ‘a’. I have a function in my player that increases the scale and this is how it goes:

public void increasePlayerSize(float factor)
{
    currentScale = gameObject.transform.localScale;
    gameObject.transform.localScale += new Vector3(factor, factor, factor);
}

‘a’ has an onTrigger script that triggers this function whenever an object with the tag player collides with it

private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "Player")
    {
        player.increasePlayerSize(increaseSizeFactor);
        logic.level++;
        gameObject.SetActive(false);
    }
}

But this is not working and i dont know why.

I had another version of this where the triggering and scaling happened on the Player itself.

public void increasePlayerSize(float factor)
{
    currentScale = gameObject.transform.localScale;
    gameObject.transform.localScale += new Vector3(factor, factor, factor);
}

private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "Upgrade")
    {
        increasePlayerSize(factor);
        logic.level++;
    }
}

This was working but i wanted to move the UPGRADE part to its own separate script because UPGRADE is another game object and i dont want it interfering with player scripts

1 Like

Hi,

If you need fast help, the best would be to join our official Discord server.

Unfortunately, I don’t know in which course and lecture you are, so I cannot even guess what your goal is/was and what “this is not working” means in this context. The only thing I can suggest is to add Debug.Logs to your code to see if you get the expected values at runtime.

1 Like

Hi!

First, as Nina said, if you need urgent help the Discord server is the way to go. Also, even tho I’m flattered you contacted me directly, I’m not part of the Gamedev staff, so I don’t always have time to answer questions, keep that in mind when you have an urgent issue, but if you are ok with waiting a little bit of time I’ll be more than glad to answer any questions you might have to the best of my capabilities. Now, unto your problem.

I need to see what kind of error this is throwing to give you an accurate response, but I’ll give you two possible answers, if none helps you, take a screenshot of your console and post it here.


If there’s no error in the console.

The function OnTriggerEnter looks for a trigger collider, if your player object doesn’t have any, the function won’t run, that’s why you aren’t getting any error message in the first place, because there’s no trigger collision.

To solve this you can either add a collider to your player object and mark the trigger checkbox, or you can change the function to “OnCollisionEnter”, this way it will search for solid colliders instead of triggers.

Check this if you are having issues understanding how collisions interact with each other.


If there’s a null reference error in your console.

This means that you probably forgot to add a line that sets the player variable in your “object a” script. You can either grab the component once the collision happens, or before, using the GameObject.FindWithTag function, this is way faster than using the FindObjectOfType<T>() function, you’ll just need to add a tag to your player object.


I also highly recommend watching the video I linked below, it might be a little bit advance, but it can help you structure your code. I’m telling you this because having a OnTriggerEnter or OnCollisionEnter on several objects might end up being quite taxing because those functions run every fixed update, this makes your second solution far better than the first.

As a rule of thumb, the less ticks (Update, LateUpdate, FixedUpdate, OnCollsionEnter, OnCollisionStay, OnCollisionExit, and so on) you have running in your game, the better.

Hope this helps!

Im really sorry but I was desperate tbh. I’m participating in a game Jam and i was running out of time.

I can’t use my discord account cause it got hacked lol so I had to resort to sending messages personally and HOPE that someone would respond if and when they see the messages

I hope I didn’t disturb you

And btw, if the on trigger command has to work, do both player and ‘a’ need to have triggers?
My player has a character controller and I thought it took care of the collisions.

Oh no! Not at all. I just wanted you to know that I’m currently not the most reliable person in the forums.

Character controller does handle collisions and it should work.

The other issue that you might be having is that the “Player” tag hasn’t been assigned to the Player object, or the “Player” tag doesn’t exist.

To check if your tag doesn’t exist, use the CompareTag() method instead of the equals operator gameObject.tag == "Something", the first method will return an error if the tag doesn’t match any of your current tags.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms