I've had this problem before, and I can't stand it anymore. Why and how does this happen? I'm new to C# and I don't know what I'm doing wrong.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonActivate : MonoBehaviour
{
bool Test;
Button ButtonHere;
void Update()
{
Test = true;
if (Test == true)
{
ButtonHere.interactable = true;
}
}
}
2 possible mistakes I can think of are:
Buttonhere is null or is referencing the wrong GameObject.
The script isn't attached to anything, try attaching it to something like the button itself or the 'Main Camera'.
Related
i followed the official unity tutorial on how to put bannner ads on my game, i set everything up in the unity dashboard, and in the editor i see a rectangle that says the ad should be there, but when i build the game nothing shows up, here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class bannerad : MonoBehaviour
{
bool testMode = false;
public string gameId = "******" (my game id is here i just dot want to share it);
// Start is called before the first frame update
IEnumerator Start()
{
Advertisement.Initialize(gameId, testMode);
while (!Advertisement.IsReady("Lost_Ad"))
{
yield return null;
}
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Show("Lost_Ad");
}
}
anyone knows what is the solution?
Are you ever calling this function? If you just replaced the Start function with this IEnumerator it will not get called unless you call it.
private void Start()
{
Advertisement.Initialize(gameId, testMode);
StartCoroutine(StartAds());
}
private IEnumerator StartAds()
{
// your code here
}
Could someone tell how could I add to a variable every second 1?
Example:
You can try coroutines out:
using System.Collections;
using UnityEngine;
public class SecondCounter : MonoBehaviour
{
void Start()
{
StartCoroutine(logEverySecond());
}
IEnumerator logEverySecond() {
while (true) {
Debug.Log("Tick");
yield return new WaitForSeconds(1);
}
}
}
Attach this script to gamobject in the scene and see in the console how the ticker logs.
Threre you could SimpNum++
I am using unity 2018.1.0f2 and windows 10. Below is the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech;
public class Dinovoice : MonoBehaviour {
KeywordRecognizer KeywordRecognizerObj;
void Start () {
string[] keywords_array={"Jump","Run"};
KeywordRecognizerObj= new KeywordRecognizer(keywords_array);
KeywordRecognizerObj.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
KeywordRecognizerObj.Start();
Debug.Log("Started");
}
void Update() {
}
private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
Debug.Log("Ended");
}
}
When I run from unity Started is getting printed.Ended is not getting printed at all when I speak.I am using unity with wikitude. I am adding this script to my prefab.Can you please help me out.Is there any setting I am missing?
Thanks
I have 10 sliders that have text components attatched to them, they are supposed to display the slider values and save the values to playerprefs. This all works perfectly except that some of the text boxes will not update/display their text when the scene is played again. Half of the text boxes populate their text values with their saved value from playerprefs, the other half return null, even though they're value is being saved properly.
here is my code to save the values (attached to each slider):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveSliderValue : MonoBehaviour {
public Slider Slider;
public float valueofslider;
void Start()
{
valueofslider = PlayerPrefs.GetFloat(gameObject.name + "valueofslider");
Slider.value = valueofslider;
}
void Update()
{
valueofslider = Slider.value;
if (Input.GetKeyDown(KeyCode.S))
{
PlayerPrefs.SetFloat(gameObject.name + "valueofslider", valueofslider);
Debug.Log("save");
}
}
}
and to display the values (attached to each text component)::
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class showvalue : MonoBehaviour {
Text percentageText;
// Use this for initialization
void Start () {
percentageText = GetComponent<Text>();
Debug.Log(percentageText);
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}
and the error:
Variable percentagetext is not set.
UnityEngine.Debug:Log(Object)
showvalue:textUpdate(Single) (at Assets/showvalue.cs:24)
UnityEngine.UI.Slider:set_value(Single)
SaveSliderValue:Start() (at Assets/SaveSliderValue.cs:19)
pictures - for understanding
and if I remove the debug.log I get a null reference.
The order of execution for Start functions can't be relied upon. Rename the showvalue Start function to Awake and see if it helps.
Basically what happens is:
some of showvalue instances execute their Start function earlier than their corresponding SaveSliderValue
thus they set the value of the text correctly
and for some that order is broken (because Start functions execute in arbitrary order) => your error
Awake is always executed before Start - use that to your advantage.
working code for the ShowValues script here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class showvalue : MonoBehaviour {
Text percentageText;
// Use this for initialization
void Awake () {
percentageText = GetComponent<Text>();
//percentageText = GameObject.Find("ThePlayer").GetComponent<SaveSliderValue>().valueofslider;
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}
I have a script inside a child gameobject , I want to disable it through coding on the parent Object ,help will be much appreciated,thanks.
edit:the script I want to carry out all the actions is not attached to the parent gameobject.
I hope this code is self commenting ;)
using UnityEngine;
using System.Collections;
public class ScriptAttachedToParent : MonoBehaviour
{
void Start ()
{
transform.GetChild (0).GetComponent<ScriptAttachedToChild> ().enabled = false;
// OR
//transform.Find("ChildGameObject").GetComponent<ScriptAttachedToChild> ().enabled = false;
}
}