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!
“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.
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");
}
}
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)
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…
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.