[Help]How to transform a object with c#

hey guys I was wondering if ENYONE new the answer to this because I’m trying to transform the player when a button is clicked so I tried this and it didn’t work. heres the code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GiantModeActivate : MonoBehaviour
{
public Button GiantMode;
GameObject Player;
void Start()
{
Button btn = GiantMode.GetComponent();
btn.onClick.AddListener(TaskOnClick);
Player = GameObject.Find(“Player”);
}
void TaskOnClick()
{
NewMethod();
}
private void NewMethod()
{
Player.gameObject.transform.localScale += new Vector3(50, 50, 50);
}
}
Thanks!

You seem to be new. Read that and do tutorials.

2 Likes

Please use code tags: Using code tags properly - Unity Engine - Unity Discussions Code is very hard to read without them :slight_smile:

You’ll need to provide more information for us to help - “didn’t work” doesn’t give enough information…

What didn’t work?

Did you get an exception?

At first glace I expect that Player = GameObject.Find("Player") may be returning null. But I’ve only very quickly glanced at the code.

“It didn’t work” isn’t much information. You’ll need to describe what happened, and how it’s different to what you wanted to happen. Also tell us what appeared in the Console window when you tried to run the code. It’s hard to diagnose an issue when we don’t know specifically what it is.

Also, this should be in the Scripting area, not under General Discussion.

Related, there’s a pretty good article in linked in my signature you should read.

2 Likes

yeah okay, sorry it is returning null and then when I start the actuall game the button wont even work.(like you click the button and nothing happens, also what I wanted to happen was for the size of the player game object to change) I even did this

[code=CSharp]using UnityEngine;
using UnityEngine.UI;


public class GiantModeActivate : MonoBehaviour
{
    public Button GiantMode;
    GameObject player;
 
    // Update is called once per frame
    void Update()
    {
        Button btn = GiantMode.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }
    //for button
    void TaskOnClick()
    {
        player = GameObject.Find("lol");
        player.gameObject.transform.localScale += new Vector3(50, 50, 50);
        //Debug.Log("Clicked");

    }
}

Thanks!
[/CODE]

I know why it didn’t :)working before I posted my last post because I didn’t even have the player game object in the game yet. (the one before the last code I posted)

1 Like

Don’t add the thing in update either. You’re getting component every frame. Slow as hell. And assigning it every frame too. Just do it at the Start.

It could be worse than assigning every frame - it might be adding it as an additional listener every frame. So after 5 frames, a button click might raise the event 5 times…

I can’t do that because I need for it to work when I play

I tried it already

Yes you can… there’s no excuse for that, it’s just bad code.

I strongly suggest heading over to https://unity3d.com/learn

You’ll hit stumbling block after stumbling block trying to learn Unity by trial-and-error.

3 Likes
using UnityEngine;
using UnityEngine.UI;

public class GiantModeActivate : MonoBehaviour {

    public Button GiantMode;
    GameObject player;

    void Start() {
            player = GameObject.Find("lol");
         
            GiantMode.onClick.AddListener (delegate { TaskOnClick(); });
    }

    //for button
    public void TaskOnClick() {
        player.gameObject.transform.localScale += new Vector3(50, 50, 50);
        //Debug.Log("Clicked");
    }
}

Using Start() won’t stop things from working when you hit the play button. For objects already in the scene Start() gets called after you hit play, before Update() is called for the first frame.

Honestly, just do this. Start with the beginner scripting stuff and take your time working through it all and making sure you actually understand it.

Yeah, YouBuggy, Remember:

Awake
OnEnable
Start
FixedUpdate
Update
LateUpdate

Well thanks guys, I’m sort of new to using unity but new to code but unity code is kinda weird

*Not new to code

Thanks! I’ll just modify it to my needs

Further edited. Was late last night and I overlooked some errors.

T

Thanks!