using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileBackground : MonoBehaviour
{
public GameObject[] cookies;
// Start is called before the first frame update
void Start()
{
Initialize();
}
// Update is called once per frame
void Update()
{
void Initialize(){
int cookieToUse = Random.Range(0, cookies.Length);
GameObject cookie = Instantiate(cookies[cookieToUse], transform.position, Quaternion.identity);
cookie.transform.parent = this.transform;
cookie.name = this.gameObject.name;
}
}
}
this is the two errors that are coming up in VS
Initialize(); error code is The name 'Initialize' does not exist in
the context (CS0103)
'Random' is an ambiguous reference between 'UnityEngine.Random' and
'System.Random' (CS0104) any suggestions
The first error happens because you are defining the method Initialize() inside the method Update(). Doing that way, you cannot use Initialize() outside of the method Update().
The second error happens because Unity is founding two Random classes. One is in the namespace System and the other is in UnityEngine. You must tell Unity which one to use.
Below is the code snipped with the corrections.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileBackground : MonoBehaviour
{
public GameObject[] cookies;
// Start is called before the first frame update
void Start()
{
Initialize();
}
// Update is called once per frame
void Update()
{
}
void Initialize(){
int cookieToUse = UnityEngine.Random.Range(0, cookies.Length);
GameObject cookie = Instantiate(cookies[cookieToUse], transform.position, Quaternion.identity);
cookie.transform.parent = this.transform;
cookie.name = this.gameObject.name;
}
}
Related
I was coding AI for enemies in a game I’m making in Unity, but then Unity gave me this error. It says the error is on line 21, but I can't find it. Any help is appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
public float lookRadius = 10f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
You have a closing bracket after the empty Update() function that doesn't belong there, it should be below OnDrawGizmosSelected() so that it encloses the EnemyController class as follows:
public class EnemyController : MonoBehaviour
{
public float lookRadius = 10f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}
I am trying to make it so that there is a counter for the amount of enimies in the current level, and I found some code that should check the amount of enimies left. After you kill them, a destroy(gameobject) command is in place. For some reason, whenever I try to use this, the text does not update and stays on "new text".
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class text : MonoBehaviour
{
public TextMeshProUGUI txt;
private GameObject[] getCount;
public float count = 0f;
// Start is called before the first frame update
public void Start()
{
}
// Update is called once per frame
void Update()
{
getCount = GameObject.FindGameObjectsWithTag("enemy");
count = getCount.Length;
txt.SetText(count.ToString());
}
}
I have been trying to make an OnTriggerEnter Score system and it has not been updating and is showing no errors so am I doing something wrong that I dont know about here is my Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ScoreManger : MonoBehaviour
{
public TextMeshProUGUI MyscoreText;
private int ScoreNum;
// Start is called before the first frame update
void Start()
{
ScoreNum = 0;
MyscoreText.text = ScoreNum.ToString();
}
void update() {
MyscoreText.text = ScoreNum.ToString();
}
public void OnTriggerEnte2D(Collider2D col){
if(col.tag == "Score"){
ScoreNum += 1;
Debug.Log("It Worked");
MyscoreText.text = ScoreNum.ToString();
Destory(col.gameObject);
}
}
}
You spelled OnTriggerEnter2D wrong.
Or you haven't marked at least one of the colliders you are interacting with as a trigger.
Or you haven't attached the script to a gameobject.
my snippets doesn't work in vscode , i use Unity 3.5 and VScode lastest version
and public class not show in gameobject
there is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class kontrol : MonoBehaviour
{
public float berat, tinggiLoncat;
public private void OnParticleCollision(GameObject bird) {
};
// Start is called before the first frame update
void Start()
{
}
void OnMouseDown() {
bird.GetComponent<Rigidbody2D> ().gravityScale = berat;
bird;GetComponent<Rigidbody2D> ().velocity = new Vector2 ( bird;GetComponent<Rigidbody2D>().velocity.x, tinggiLoncat);
}
// Update is called once per frame
void Update()
{
}
}
This isnt working because you have many syntax errors and the script isn't compiling so you cant add it obviously.
public private void OnParticleCollision(GameObject bird) { // Public Private? Choose one!
}; //no semicolon after methods
bird.GetComponent<Rigidbody2D> ().gravityScale
bird;GetComponent<Rigidbody2D> ().velocity //why are you using a semicolon, you did it right in the first place.
GetComponent is expensive. Use it once in Awake or Start, and save the reference. For example:
class SomeComponent: MonoBehaviour {
Rigidbody rb;
void Awake() {
rb = GetComponent<Rigidbody>();
}
void Update() {
// do something with rb
}
}
You're using semicolons instead of periods.
I just want to move a bullet (but it doesn't work) and test on cube (but the code too did not move the cube).
The commented codes also did not move the game object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movetest : MonoBehaviour
{
//public GameObject cube;
// Use this for initialization
public GameObject testmovecube;
public Rigidbody rb;
void Start () {
//testmovecube = this.GetComponent<GameObject>();
//rb = testmovecube.GetComponent<Rigidbody>();
move();
//
}
// Update is called once per frame
void Update () {
}
private void move()
{
testmovecube.transform.position =
Vector3.MoveTowards(transform.position, new Vector3(226, 1, 226) , 2);
//transform.Translate(Vector3.forward);
//rb.AddForce(Vector3.forward);
}
}
Please any help is appreciated.