I'm trying to play/stop animations with C# codes but they didn't worked, Javascript code work (legacy mode). I hope someone know where is problem.
C#:
using UnityEngine;
using System.Collections;
public class PlayStop : MonoBehaviour
{
public GameObject Cube;
void PS()
{
GetComponent<Animation>().Play();
}
}
JS:
var Cube : GameObject;
Cube.GetComponent.<Animation>().Play();
C# Screenshot
JS Screenshot
The problem is that mentioned C# and JS code you wrote are not equivalent.
In C# code you call GetComponent<Animation> to search in gameObject to which PlayStop script is attached to, while in JS script you are searching with GetComponent<Animation> in Cube gameObject you dragged from inspector.
You should try:
using UnityEngine;
using System.Collections;
public class PlayStop : MonoBehaviour
{
public GameObject Cube;
void PS()
{
//Will search in Cube game object for component of Animation type
Cube.GetComponent<Animation>().Play();
}
}
To run code inside PS() on application run, rename it to Start() or move code from inside of it to new method and name it Start().
To run PS() on button click, click + in OnClick section of Button in Inspector, drag your PlayStop script to box on the left and choose the function you want to call on click from dropdown, like this:
Related
Now I working on project like black mirror banderanatch. I create a movie that player has to interact with by pressing a specific key on keyboard to change to another scene. I dont know how to code it, I don't need a box or anything to click because in video I will say what button to press to go on another scene. I've tried put the video in UI in each scene but its does not work
some of the code I'm using
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeSceneWithButton : MonoBehaviour
{
public void LoadScene(string sceneName1)
{
SceneManager.LoadScene(sceneName1);
}
if{keyboard.write() a;
LoadScene(sceneName1)}
}
what should I add to make it change scene with specific button
ps.first time using unity any advice please teach me
You need to check the input in the Update function, like this:
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeSceneWithButton : MonoBehaviour
{
[SerializeField] private string _sceneName;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) SceneManager.LoadScene(_sceneName);
}
}
I have a button in my Unity2D game that the user can click and the scene will start over. On my button, I have a script attached with this code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Restart : MonoBehaviour
{
public void PlayAgain()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
Debug.Log("Play Again Works");
}
}
I know that the button is working and the Play Again function is working because the Debug.Log is working. However, when I click the button the scene doesn't restart. When I click the button, in the Unity hierarchy, next to the scene name it very briefly shows "(not loaded)". My scene is showing up in the Build settings. Does anyone know what could be causing this? Thanks for your help.
First check that you have added the scene in your build settings. If that is not your problem try following code.
SceneManager.LoadScene (SceneManager.GetActiveScene().name);
Good day everyone,
how can I make another scene after scanning the image target? So far I can only do the buttons which are the basics and am currently working with c#, visual studio, unity and vuforia.Iv'e done the main menu screen which has the buttons scan and exit. Whenever you click the scan button it will automatically go to the AR camera and my question is how can I make a AR camera which you can scan an image and go to the another scene. No animation or whatsoever, just scan the image and go to another scene. Does someone has an idea of how to make it happen?
so far this is what iv'e coded:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour {
public void System()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
}
public void Back()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex -1);
}
public void Exit()
{
Debug.Log("You have exited the app");
Application.Quit();
}
}
In your case you can just create a gameobject as a child of your image target, attach a script, lets say sceneloader.cs and put in the start() method your load-scene-code....but this are total basics. Did you even read the documentation how image targets work?
Title says everything... How to reference image effects attached to a main camera via script
First, add using UnityStandardAssets.ImageEffects; to the top of your script. The rest is easy. You use Camera.main to access the main camera. You can then use GetComponent to get the Image Effect script instance.
using UnityEngine;
using UnityStandardAssets.ImageEffects;
public class MyScript : MonoBehaviour
{
void Start()
{
Bloom bloomEffect = Camera.main.GetComponent<Bloom>();
Blur blurEffect = Camera.main.GetComponent<Blur>();
Tonemapping toneMappingEffect = Camera.main.GetComponent<Tonemapping>();
}
}
I needed to create UIButton attached to a gameobject.
I'm using a board based game i.e., when ever clicking a button at that time place one object to that particular button.
I am using c# script in unity3d .
void UIBtn(GameObject BName)
{
//here to write Button click event.
}
I'm assuming you mean GUI.Button .
Reading your first sentence I understood that you wanted to create a button where a GameObject is, but reading your second sentence it seems that you want a GameObject to appear when clicking a button. Since I'm not sure, I'll answer both.
To make a GUI button appear at the place the mouse is use something like:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void OnGUI() {
Vector2 screenPos = Event.current.mousePosition;
GUI.Button ( new Rect(screenPos.x,screenPos.y,100,100),"Hello");
}
}
Attaching a button to a GameObject requires first identifying the GameObject via Physics.Raycast and then getting the GameObject from the HitCollider and then, on the game object's OnGUI loop. constantly translate its world coordinates to screen coordinates to be able to show a button, via GUI.Button.