I was just copying a tutorial on YouTube on how to make the pong game and when it came to scripting the ball, there was an error for me but not the guy in the video.
I have no experience in C#, only Python. This is the error message:
Assets\Ball.cs(24,26): error CS0117: 'Random' does not contain a definition for 'range'
Anyone know what's wrong?
This is the script for it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float speed;
public Rigidbody2D rb;
//Start is called before the first frame update
void Start()
{
Launch();
}
//Update is called once per frame
void Update()
{
}
private void Launch()
{
float x = Random.range(0,2) == 0 ? -1 : 1;
float y = Random.range(0,2) == 0 ? -1 : 1;
rb.velocity = new Vector2(speed * x, speed * y);
}
}
You have to write Random.Range and not Random.range
You need to capitalize "range", Random.Range is a method and most methods start with a capital letter so instead of "Random.range", you need to type "Random.Range"
Here the script method just calls the "Launch()" method at initialization time, your "Lauch()" method is problematic, you can try to modify the "Random.range(0,2)" method to "Random.Range(0, 2)"
Related
Got 2 errors from 1 script and dont know whats happening.
If anyone can help that would be great.
Error is in the title and the other one is the same except instead of 'Start' its 'Update'
Thanks for reading!!
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float health = 50f;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
Vector3 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
float y = Quaternion.identity.eulerAngles.y;
float z = Quaternion.identity.eulerAngles.z;
rb.rotation = Quaternion.Euler(angle, y, z);
}
}
Using the override operator should get rid of the compiler errors, but I'm not a Unity dev so I'm not sure if Unity will treat your class in the correct way. Here's how you would do it:
override void Start()
{
...
}
// Update is called once per frame
override void Update()
{
...
}
You can read more about the override operator here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/override
I also found this article and it declares Start as an IEnumerator rather than a void so that may fix it too.
So I'm making a crap version of flappy bird just for pratice and I want to spawn pipes in increments based on time, so I can then later start removing time to make it prgressively harder.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pipe_Spawner : MonoBehaviour
{
public GameObject pipesPrefab;
// Start is called before the first frame update
void Start()
{
}
public float secsToNext -= Time.deltaTime// public lets you check it running in the inspector
{
// Update is called once per frame
void Update()//every single frame its called
{
secsToNext -= Time.deltaTime; // T.dt is secs since last update
if(secsToNext<=5) {
secsToNext = Random.Range(8.0f, 12.0f);
float Vertical = Random.Range(1.0f, 6.0f);
float Horizontal = Random.Range(-7.0f, 7.0f);
Instantiate(pipesPrefab, new Vector2(Vertical,Horizontal), Quaternion.identity);
}
}
}
}
my errors showing up in unity are:
Assets\pipe_Spawner.cs(13,29): error CS1003: Syntax error, ',' expected
Assets\pipe_Spawner.cs(13,32): error CS1002: ; expected
Assets\pipe_Spawner.cs(14,5): error CS1519: Invalid token '{' in class, struct, or interface member declaration
Assets\pipe_Spawner.cs(27,1): error CS1022: Type or namespace definition, or end-of-file expected
In that order if it matters
right now the coordinates are random purely for testing purposes.
you have to declare the variable secsToNext first before you subtract something from it. i would recommend to spawn a set of pipes at the start and also set the value for secsToNext in the start method.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pipe_Spawner : MonoBehaviour
{
public GameObject pipesPrefab;
public float secsToNext;
void Start() //Instantiating the pipes and set the secsToNext at the start
{
secsToNext = Random.Range(8.0f, 12.0f);
float Vertical = Random.Range(1.0f, 6.0f);
float Horizontal = Random.Range(-7.0f, 7.0f);
Instantiate(pipesPrefab, new Vector2(Vertical,Horizontal), Quaternion.identity);
}
void Update()
{
secsToNext -= Time.deltaTime; // T.dt is secs since last update
if(secsToNext <= 5)
{
secsToNext = Random.Range(8.0f, 12.0f);
float Vertical = Random.Range(1.0f, 6.0f);
float Horizontal = Random.Range(-7.0f, 7.0f);
Instantiate(pipesPrefab, new Vector2(Vertical,Horizontal), Quaternion.identity);
}
}
}
Im trying to make my character jump in unity2D,the jump itself works but i cant properly check if my player touches the ground.
I get 2 errors in the unity console.
the first one :
Physics2D does not contain a defenition for OverLapArea.
the second error :
Vector2 does not contain a constructor that takes 3 arguments.
(also i apologise for my bad english,its not my native language)
The script :
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class jump : MonoBehaviour
{
public bool IsGrounded;
public LayerMask platfomLayer;
public Rigidbody2D rb2D ;
// Start is called before the first frame update
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
IsGrounded = Physics2D.OverLapArea (new Vector2(transform.position.x - 0.5f, transform.position.x - 0.5f),
new Vector2(transform.position.x + 0.5f, transform.position.y - 0, 51f),platfomLayer);
{
if (Input.GetKeyDown("space") && IsGrounded) rb2D.AddForce(transform.up * 2000f);
}
}
}
The errors are both correct.
OverLapArea is spelled correctly but capitalized wrong. Unity references are case-sensitive.
You are indeed trying to pass 3 values into a new Vector2. Look carefully at what you're passing into your second new Vector2 value. You have three values separated by commas within the parenthesis. It looks like you mis-typed "0.5f" as "0, 51f"
I'm making a 2D platformer, and I want the camera to follow the character once it gets to the center, but follow it only on the x axis. I've downloaded this code, and it worked, but it followed the character on both the x and the y, and it stuck the character in the same corner of the camera that it started. I tried to add an if statement so that the camera only started moving once the offset was equal to the player, but it didn't work. I got the error code 'cannot implicitly convert vector3 to bool'
Here is the code:
using UnityEngine;
using System.Collections;
public class CompleteCameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
if (offset == player.transform.position)
{
transform.position = player.transform.position + offset;
}
}
}
The documentation seems to suggest that what you are doing is fine. But perhaps your code could be modified to fit this example? My gut says that this is going to give you the same error, but it could be worth trying.
https://docs.unity3d.com/ScriptReference/Vector3-operator_eq.html
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform other;
void Example()
{
if (other && transform.position == other.position)
{
print("I'm at the same place as the other transform!");
}
}
}
You could also try using the Vector3.Equals() method instead if you're ok with doing an exact comparison.
https://docs.unity3d.com/ScriptReference/Vector3.Equals.html
You could also try implementing your own function to compare the two vectors.
https://answers.unity.com/questions/395513/vector3-comparison-efficiency-and-float-precision.html
I really can't see anything wrong with the code you've posted, is it possible the error exists in another file?
I'm trying to destroy my player whenever the explosion particle effect comes within a certain distance of the player. Here is what I tried and this script was added to my explosion prefab:
using UnityEngine;
using System.Collections;
public class ExplosionController : MonoBehaviour {
GameObject playerObj;
Transform player;
Transform playerPos;
// Use this for initialization
void Start () {
player= GameObject.FindGameObjectWithTag("Player").transform;
playerObj = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
float playerDis = Vector3.Distance(player.position, transform.position);
if (playerDis == 4)
{
Debug.Log(playerDis);
Destroy(playerObj);
}
}
}
My Debug attempt at the bottom condition didn't yield any console output, so I assume I made a mistake in the playerDis variable.
I can't comment because I don't have 50 reputation points yet, but I would change your
if (playerDis == 4)
to
if (playerDis <= 4)
This should catch any collisions that may not exactly equal 4.00.... because Update may not be called fast enough.
With the keyword in your question being 'within'.. You should look for a distance from 0 to 4. So:
void Update()
{
float playerDis = Vector3.Distance(player.position, transform.position);
if (playerDis <= 4)
{
Debug.Log(playerDis);
Destroy(playerObj);
}
}
I assume that your distance is never exactly equal to 4, considering that Vector3.Distance() returns a float.