I am making a tetris game with unity but this code is not spawning my tetrimino. I have assigned my gameobjects to the group[] array.
This is my code:
using UnityEngine;
using System.Collections;
public class Spawner1 : MonoBehaviour {
public GameObject[] group;
void start(){
SpawnNext ();
}
void SpawnNext(){
Instantiate(group[Random.Range(0,group.Length)],new Vector2(5.0f,10.0f),Quaternion.identity);
}
}
It's not spawning because you used lower-case s in start. This should be Start not start. Please fix that and your object should now start spawning.
public class Spawner1 : MonoBehaviour {
public GameObject[] group;
void Start(){
SpawnNext ();
}
void SpawnNext(){
Instantiate(group[Random.Range(0,group.Length)],new Vector2(5.0f,10.0f),Quaternion.identity);
}
}
Related
This code here doesn't work, I don't get any errors, but the scripts doesn't print "End" in the console when my player hits something, here's the code:
using UnityEngine;
public class GameManager : MonoBehaviour
{
public void End(){
Debug.Log("End");
}
}
and on another script:
using UnityEngine;
public class PlayerCollision3 : MonoBehaviour
{
public Move movement;
void OnCollisionEnter (Collision collisionInfo) {
if (collisionInfo.collider.tag == "obsik"){
movement.enabled = false;
FindObjectOfType<GameManager>().End();
}
}
}
I'm creating a game and i want to show a panel when the player is dead
I've tried different approaches but none seems to do what I want
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DeadOrAlive : MonoBehaviour
{
public GameObject Player;
public GameObject deadPanel;
void Update()
{
if (!GameObject.FindWithTag("Player"))
{
deadPanel.SetActive(true);
}
}
}
To check if a object has been destroyed, you should use MonoBehavior's OnDestroy like so:
// Attach this script to the player object
public class DeadOrAlive : MonoBehaviour
{
public GameObject deadPanel;
void OnDestroy()
{
deadPanel.SetActive(true);
}
}
You can also instead of destroying the player object, set it to active/inactive, but to check if the player is dead or alive this way, you will need a separate object which checks the active state:
//Attach this to a object which isn't a child of the player, maybe a dummy object called "PlayerMonitor" which is always active
public class DeadOrAlive : MonoBehaviour
{
public GameObject deadPanel;
void Update()
{
if (!GameObject.FindWithTag("Player"))
{
deadPanel.SetActive(true);
}
}
}
Haven't used unity in a while and forgot how weird it could get.
Thanks to #VincentBree this is how I did it
void Update()
{
if (!Player.activeSelf)
{
deadPanel.SetActive(true);
}
}
I'm creating a game and i want to show a panel when the player is dead
I've tried different approaches but none seems to do what I want
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DeadOrAlive : MonoBehaviour
{
public GameObject Player;
public GameObject deadPanel;
void Update()
{
if (!GameObject.FindWithTag("Player"))
{
deadPanel.SetActive(true);
}
}
}
To check if a object has been destroyed, you should use MonoBehavior's OnDestroy like so:
// Attach this script to the player object
public class DeadOrAlive : MonoBehaviour
{
public GameObject deadPanel;
void OnDestroy()
{
deadPanel.SetActive(true);
}
}
You can also instead of destroying the player object, set it to active/inactive, but to check if the player is dead or alive this way, you will need a separate object which checks the active state:
//Attach this to a object which isn't a child of the player, maybe a dummy object called "PlayerMonitor" which is always active
public class DeadOrAlive : MonoBehaviour
{
public GameObject deadPanel;
void Update()
{
if (!GameObject.FindWithTag("Player"))
{
deadPanel.SetActive(true);
}
}
}
Haven't used unity in a while and forgot how weird it could get.
Thanks to #VincentBree this is how I did it
void Update()
{
if (!Player.activeSelf)
{
deadPanel.SetActive(true);
}
}
In unity, I'm trying to make a press of a button increase the speed of the player. However, each time I run it. It gives me:
Error CS0120: An object reference is required for the non-static
field, method, or property 'PlayerController.speed'
I've already tried changing order of the code, so what could I do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Upgrader1 : MonoBehaviour
{
void Start()
{
GameObject Player = GameObject.Find("Player");
PlayerController PlayerController = Player.GetComponent<PlayerController>();
}
public void Upgrade1()
{
PlayerController.speed++;
}
}
public class Upgrader1 : MonoBehaviour
{
PlayerController PlayerController; //It should be member variable
void Start()
{
GameObject Player = GameObject.Find("Player");
PlayerController = Player.GetComponent<PlayerController>();
}
public void Upgrade1()
{
PlayerController.speed++;
}
}
it's always a good thing to use proper naming conventions.
PlayerController _PlayerController;
void Start() {
GameObject Player = GameObject.Find("Player");
_PlayerController = Player.GetComponent<PlayerController>();
}
public void UpgradeSpeed() // I changed the name according to its functionality
{
_PlayerController.speed++;
}
With this, you won't put PlayerController class reference again by mistake.
I was using this tutorial to code a VR game in Unity. When I got to the scripting, I ran into an issue. I could not see variables in the Inspector sidebar. Here is my code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hello : MonoBehaviour {
[SerializeField]
public AudioClip clip;
public AudioSource audioSource;
public Transform gunBarrelTransform;
void Start ()
{
audioSource = GetComponent<AudioSource> ();
audioSource.clip = clip;
}
void Update (){
if (OVRInput.Get (OVRInput.Button.SecondaryHandTrigger)) {
audioSource.Play ();
RaycastGun ();
}
}
private void RaycastGun()
{
RaycastHit hit;
if (Physics.Raycast (gunBarrelTransform.position,
gunBarrelTransform.forward, out hit)) {
if (hit.collider.gameObject.CompareTag ("Cube")) {
Destroy (hit.collider.gameObject);
}
}
}
}
Even when I remove the [SerializeField] it does not work. Does anybody know why the variables aren't showing up? Thank you so much for the help!