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;
}
}
Related
This animator script works, however, in place of the Keycode input inside the WHILE LOOP, I need to use a UI button for mobile, which I haven't been able to figure out. I found an answer about putting a wrapper around it to make method available to click event, but have no idea how that's supposed to work within update function. It took me a long time to get this far, being a newbie to unity AND c#, so if you could provide a detailed answer or suggestion, I can get my life back, please help if you can.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using SWS;
public class goat_question : MonoBehaviour
{
private Animator anim;
public GameObject player;
public Text ResultText;
public Text AnswerText;
public Text AnswerText2;
public Button GoatButton;
void Start()
{
anim = GetComponent<Animator>();
Button btn = GoatButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
Debug.Log("You have clicked the button!");
}
// Update is called once per frame
void Update()
{
if (AnswerText.text.Equals(AnswerText2.text))
{
StartCoroutine(GoatWalkPathCoroutine());
}
IEnumerator GoatWalkPathCoroutine()
{
while (true)
{
if (Input.GetKeyDown(KeyCode.K))
{
anim.Play("goat_hi_walk");
player.GetComponent<splineMove>().enabled = true;
yield return new WaitForSeconds(27);
anim.Play("goat_hi_licking");
}
yield return null;
}
}
}
}
In a separate script for just the UI button, have a bool called isClicked or something, and when the button gets clicked set that to true. In this main script, you can reference the one you just made, and instead of the Input.GetKey, you can say, if(otherScript.isClicked).
This was originally coded in unity 2018 then ported to unity 2019 because my main project is on that version. It works perfectly in unity 2018 (aside from that button.clicked += used to be button.AddListener())
The only relevant bit of the code is lines 12 - 17 but who knows.
Script is attached to object with the Button component.
Unity 2019.4.11f1
Returns the following error with the setup listed above:
ArgumentException: GetComponent requires that the requested component 'Button' derives from MonoBehaviour or Component or is an interface.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using UnityEngine.Events;
public class ButtonProperties : MonoBehaviour {
public ButtonType buttonType;
public string dest;
Button button;
void Start()
{
button = GetComponent<Button>();
//perform different functions dependant on the button type
if (buttonType == ButtonType.Scene)
{
//add ButtonLoadScene as a listener to the the button
button.clicked += ButtonLoadScene;
}
else if (buttonType == ButtonType.Menu)
{
button.clicked += ButtonLoadMenu;
}
//change button text to "Load Scene" + the name of the scene
string str = "Load " + dest;
GetComponentInChildren<TextElement>().text = str;
}
//private scene loader because event listeners cant take arguments >:(
void ButtonLoadScene()
{
SceneManager.LoadScene(dest);
}
void ButtonLoadMenu()
{
//array of all canvases in the scene
Canvas[] canvases = GameObject.FindObjectsOfType<Canvas>();
//foreach canvas
foreach (Canvas canvas in canvases)
{
//if the canvases name is the desired name
if (canvas.name == dest)
{
//turn all canvases off
foreach (Canvas c in canvases)
{
c.enabled = false;
}
//turn current canvas on
canvas.enabled = true;
//break loop
break;
}
}
}
}
public enum ButtonType
{
Scene,
Menu
};
I also tested this by adding a NewBehaviourScript to the button with Button button; and, in the start function, Start() {button = GetComponent<Button>();}
Same error
I believe you are using the wrong namespace for your button. Try changing:
using UnityEngine.UIElements;
to
using UnityEngine.UI;
There is a Button in both namespaces, but the one in UnityEngine.UI is likely the component you're working with.
Had somebody else on the team test the build, as I started to think from the comment thread, I was missing the UI library. Changing it to the UI library was the solution but not for me until I restarted the computer. Thank you Thomas Finch for the help.
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'.
I followed a Youtube tutorial https://www.youtube.com/watch?v=afz2An4X0vw on how to use Unity networking.
The point is to have an object with a network ID component spawn the player and in order to control the player we need to give the client spawning the player authority over that player.
However, having done exactly the same as in the video, whenever I host the game (with the unity Network HUD) the hasAuthority returns false instead of true on only the host.
It returns true on all the other clients (as it should).
To me it seems as if the hosting person doesn't see itself as a client but only the server, yet this doesn't happen in the video linked above.
How do I make the hosting player return true on hasAuthority as it refuses to do so with the code below?
The script on the object (spawned when the client connects):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ThePlayerScript : NetworkBehaviour
{
public GameObject abigail;
public GameObject abigailN;
void Start ()
{
if(isLocalPlayer == false)
{
return;
}
CmdSpawnPlayer();
}
void Update () { }
[Command]
void CmdSpawnPlayer()
{
abigailN = Instantiate(abigail);
NetworkServer.SpawnWithClientAuthority(abigailN, connectionToClient);
}
}
The code on the player that is spawned by the gameobject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Abigail : NetworkBehaviour
{
public GameObject mainCamera;
Vector3 myLocation;
Vector3 cameraView;
// Use this for initialization
void Start ()
{
if(hasAuthority == false)
{
Destroy(this);
return;
}
mainCamera = GameObject.Find("Main Camera");
}
}
(I removed the Update function in the Abigail class as it is irrelevant as it doesn't get executed.)
I found out that hasAuthority in this case will only return false in the Start() method. If you use it in the Update() method it will return true properly if you assign authority simply as you do in "ThePlayerScipt" with:
NetworkServer.SpawnWithClientAuthority(abigailN, connectionToClient);
Hope that helps :)
i have created a text and under text there are two input fields. then i have created a prefabs of them and instantiated through code. and its working, but problem is the instantiated object is empty and does not show anything just a box, is there any solution to the problem i'm facing of.
Here is my code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SubmitButton : MonoBehaviour {
public GameObject objects;
public void Start () {
// Debug.Log ("ok");
}
public void InstantiateButton () {
objects.transform.position = new Vector3 (57.4f, 381.58f, 0f);
Instantiate (Resources.Load ("Prefabs/Bupivacaine"), new Vector3 (57.4f,
381.58f, 0f), Quaternion.identity);
}
}
If you're trying to display UI on the Canvas then you need to parent your prefab to the canvas.
go.transform.SetParent(canvasGo.transform, false);
To instantiate a UI object, you must make sure its a root child of a Canvas.
So you should change your instantiating method:
Instantiate(Resources.Load("Prefabs/Bupivacaine"), GameObject.Find("parentNamehere").transform);
enter the name of the parent gameobject in "parentNamehere" and remember: it must be a child of a canvas or the canvas itself.