Creating game in C# Unity3D and it doesn't work - c#

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.

Related

Unity 3D: Error CS0116: A namespace cannot directly contain members such as fields or methods

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);
}
}

Score Not Updating

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.

Match three errors

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;
}
}

Curly brackets not connecting properly

This is my code. Tried repasting the code. And multiple other things like writing the function again.
The brackets wont connect(at the start and the end of the code) when I make new function. The closing bracket from new function will jump to the one that summarizes the whole code. Even placing it properly does nothing. Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooter : MonoBehaviour
{
[SerializeField] GameObject projectile;
[SerializeField] Transform gunPos;
public void Fire()
{
if (!gunPos) { return; }
GameObject projectileInstance = Instantiate(projectile, gunPos.transform.position, Quaternion.identity) as GameObject;
}
private void SetLaneSpawner()
{
public AttackerSpawner[] spawners = FindObjectsOfType<AttackerSpawner>();
}
private void Start()
{
SetLaneSpawner();
}
public void Update()
{
if (null)
{
Debug.Log("pew pew");
//TODO attack animation
}
else
{
Debug.Log("wait");
//TODO idle animation
}
}
}
The error is in the line:
public AttackerSpawner[] spawners = FindObjectsOfType<AttackerSpawner>();
// Remove the public so it becomes.
AttackerSpawner[] spawners = FindObjectsOfType<AttackerSpawner>();
Accessibility modifiers (public, private, etc) can only be applied at the class and property level. You cannot use them inside a function.

Movetowards , Addforce , translate none is working why?

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.

Categories

Resources