I know this question has been asked before, here and here.
But I still can't get variables from another script. I don't know what I'm doing wrong.
*(I'm really new to programming in general so I might have missed something glaringly obvious)
I keep on getting the error: The name 'points' does not exist in the current context
The slimespawner script is on the Canvas.
Sorry if the question is too simple.
here's the script I'm trying to access:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class slimespawner : MonoBehaviour
{
public int points;
public Text score;
public float xx;
public float yy;
void Start()
{
points = 0;
xx = Random.Range(-32f, 32f);
yy = Random.Range(-18.5f, 18.5f);
}
void Update()
{
score.text = "Score: " + points.ToString();
}
}
And here's the script that's trying to use the points variable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class slimecontroller : MonoBehaviour
{
private float movespeed = 0.1f;
public slimespawner slisp;
void Start()
{
slisp = GameObject.Find("Canvas").GetComponent<slimespawner>();
}
void Update()
{
points += 1;
}
}
Your code should look as this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class slimecontroller : MonoBehaviour
{
private float movespeed = 0.1f;
public slimespawner slisp;
void Start()
{
slisp = GameObject.Find("Canvas").GetComponent<slimespawner>();
}
void Update()
{
slisp.points += 1;
}
}
Access the property with slisp.points += 1;
Related
I am trying make an infinite level generator that gets level parts I give to it and mix's them up to make infinite long an unique levels for me. but the problem is that When I Typed [SerializeField] private Player player; it said No namespace called "Player" which is odd because There are other code's were Player player is used and it works.Maybe I am just dumb but anyway here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGenerator : MonoBehaviour {
private const float PLAYER_DISTANCE_SPAWN_LEVEL_PART = 200f;
[SerializeField] private Transform levelPart_Start;
[SerializeField] private List<Transform> levelPartList;
[SerializeField] private Player player;
private Vector3 lastEndPosition;
private void Awake() {
lastEndPosition = levelPart_Start.Find("EndPosition").position;
int startingSpawnLevelParts = 5;
for (int i = 0; i < startingSpawnLevelParts; i++) {
SpawnLevelPart();
}
}
private void Update() {
if (Vector3.Distance(player.GetPosition(), lastEndPosition) < PLAYER_DISTANCE_SPAWN_LEVEL_PART) {
SpawnLevelPart();
}
}
private void SpawnLevelPart() {
Transform chosenLevelPart = levelPartList[Random.Range(0, levelPartList.Count)];
Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition);
lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
}
private Transform SpawnLevelPart(Transform levelPart, Vector3 spawnPosition) {
Transform levelPartTransform = Instantiate(levelPart, spawnPosition, Quaternion.identity);
return levelPartTransform;
}
}
I have tried to make in into a game object but then I got even more errors
No namespace called "Player"
It makes me think that the problem is in the Using.
In some parts of your code it accepts it because you are using it or it is in the same namespace.
Assuming it is called like this, try adding the using as follows
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Player; (it can change but should be like this)
I'm new to C# (did some stuff in python earlier) and i cant get this code to work. Im making a mobile game and this script should run a timer, check if the timer is equal to "SaleTime" and if it is add money to the users balance and reset the timer to 0.
As VSCode is not giving me any errors i dont know what the problem is and after looking around i cant find a solution to it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Threading;
public class Sales : MonoBehaviour
{
public float Timer = 0.0f;
public float SaleTime = 5.0f;
public float ProductValue = 5.0f;
public float Money = 1000.0f;
void Start()
{
StartCoroutine(time());
}
public void GameTime()
{
Timer += 1;
}
IEnumerator time()
{
while (true)
{
GameTime();
yield return new WaitForSeconds(1);
}
}
public void SaleFunction()
{
if (Timer == SaleTime)
{
Timer = 0.0f;
Money = Money + ProductValue;
}
}
}
You are not calling SaleFunction(), so the program never checks the condition.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Threading;
public class Sales : MonoBehaviour
{
public float Timer = 0.0f;
public float SaleTime = 5.0f;
public float ProductValue = 5.0f;
public float Money = 1000.0f;
void Start()
{
StartCoroutine(time());
}
public void GameTime()
{
Timer += 1;
}
IEnumerator time()
{
while (true)
{
GameTime();
SaleFunction(); // Added line.
yield return new WaitForSeconds(1);
}
}
public void SaleFunction()
{
if (Timer == SaleTime)
{
Timer = 0.0f;
Money = Money + ProductValue;
}
}
}
I'm a beginner programmer on c# and Unity.
So I was making a grid system for my game and when I'm trying to add as a component every script in my unity project it prints this one error : "The script don't inherit a native class that can manage a script"
I don't know what i need to do.Help me please if u can
//////////first script///////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gird1 : MonoBehaviour
{
private int width;
private int height;
private int[,] gridArray;
public Gird(int width, int height)
{
this.width = width;
this.height = height;
gridArray = new int[width, height];
Debug.Log(width + " " + height);
}
}
////////second script////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Testing : MonoBehaviour
{
private void Start()
{
Grid grid = new Grid(20, 10);
}
void Update()
{
}
}
image of grid
Your first script is named "Gird" not "Grid".
Second Script:
public class Testing : MonoBehaviour
{
public Gird1 gird1; // Assign in the inspector. We need an instance of Gird1
private void Start()
{
if (gird1 == null) { // In case someone forgot to assign in the inspector
gird1 = (Gird1)FindObjectOfType(typeof(Gird1)); // .. We'll search the scene
}
gird1.Gird(20,10); // Call the Gird() function on your gird1 instance
}
}
Getting this while trying to make a game on Unity, I pretty much copied this code from a source, yet it's still not working can anyone explain why?
It's underlined green and its speed;
This is also happening on another field labled backgroundElements;
public class BackgroundElement : MonoBehaviour{
[SerializeField]
private float **speed;**
public void Move()
{
transform.Translate(Vector2.left * speed * Time.smoothDeltaTime);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
[SerializeField]
private BackgroundElement[] backgroundElements;
void Start()
{
}
void Update()
{
if (true)
{
foreach (BackgroundElement element in backgroundElements)
{
element.Move();
}
}
}
}
Hello I'm currently making a game at the moment that's like pacman but multiplayer. I'm having trouble on how to network the scores. I'm wanting to show own score on Local host and own score on Client. So far I could only update scores for the Local Host. Could anyone please help?
Score Manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class ScoreManager : NetworkBehaviour {
public static int score;
Text text;
void Start()
{
text = GetComponent<Text>();
score = 0;
}
void Update()
{
if (!isServer)
{
return;
}
text.text = "Score: " + score;
}
}
Ontriggerstart for score
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PacDot : MonoBehaviour {
void OnTriggerEnter2D(Collider2D co)
{
if (co.tag == "UFO")
{
Destroy(gameObject);
ScoreManager.score++;
}
}
}
Player Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class UFOMove : NetworkBehaviour {
public float speed;
private Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if (!isLocalPlayer)
{
return;
}
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
rb2d.AddForce(movement * speed);
}
}