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);
}
}
}
Related
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)"
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 1 year ago.
So my game is a 2D top down movement game and my script does make my enemy attack but it constantly loops the attack animation because I obviously don't know what EXACTLY to put code wise to make the enemy attack when in range of the player to do damage instead of letting him constantly loop. Also i seem to be getting an error when i get close to my enemy as of right now it says
NullReferenceException: Object reference not set to an instance of an object
EnemyCombat.Attack () (at Assets/EnemyCombat.cs:36)
EnemyCombat.Update () (at Assets/EnemyCombat.cs:25)
Also, Here is the EnemyCombat script
enemy attausing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyCombat : MonoBehaviour
{
public Animator animator;
public Transform AttackPoint;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
public int attackDamage = 5;
public float attackRate = 2f;
float nextAttackTime = 0f;
// Update is called once per frame
void Update()
{
if (Time.time >= nextAttackTime)
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
void Attack()
{
animator.SetTrigger("Attack");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, attackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}
void OnDrawGizmosSelected()
{
if (AttackPoint == null)
return;
Gizmos.DrawWireSphere(AttackPoint.position, attackRange);
}
}
To fix your endless attack loop:
// Update is called once per frame
void Update()
{
if (attackRate >= nextAttackTime) /* checks that nextAttackTime is less than or equal to the attackRate */
{
Attack();
nextAttackTime = Time.deltaTime * 5f; // adds 5 seconds to nextAttackTime
}
else
{
nextAttackTime -= Time.deltaTime; /* if nextAttackTime is greater than the attackRate, subtract one from nextAttackTime. this only happens once per second because you use Time.deltaTime */
}
}
From that NullReference Error it looks like the major problem you're having is that there is no actual point in the code or in the game hierarchy that you are telling your script which gameObject it is referring to.
Line 36 is trying to retrieve that information with .GetComponent<Enemy()>, so you need to provide that reference.
You could do this in the script fairly easily by creating a public variable that you can drag the enemy gameObject into in your hierarchy in Unity.
Try something like:
public GameObject enemyObject;
This will create a variable in the script visible in the hierarchy when you select the script which you can drag the appropriate gameObject into.
There might need to be some adjustments to it because I can't see the rest of your code, but this seems to be the issue.
Another option would be trying to manually adding it in:
void Start()
{
GameObject enemyObject = gameObject.GetComponent<Enemy>();
}
this is taken from Unity Scripting Documentation
I'm getting the error NullReferenceException: Object reference not set to an instance of an object
ThirdPersonCamera.Update () (at Assets/scripts/ThirdPersonCamera.cs:24)
My Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using UnityEngine.SocialPlatforms;
using UnityEngine.UI;
using UnityStandardAssets.Utility;
public class ThirdPersonCamera : MonoBehaviour {
[SerializeField]Vector3 cameraOffset;
[SerializeField]float damping;
Transform cameraLookTarget;
Player localPlayer;
void Awake () {
GameManger.Instance.OnLocalPlayerJoined += HandleOnLocalPlayerJoined;
}
void HandleOnLocalPlayerJoined (Player player) {
localPlayer = player;
cameraLookTarget = localPlayer.transform.Find("cameraLookTarget");
if (cameraLookTarget == null) {
cameraLookTarget = localPlayer.transform;
}
}
// Update is called once per frame
void Update () {
Vector3 targetPosition = cameraLookTarget.position + localPlayer.transform.forward * cameraOffset.z +
localPlayer.transform.up * cameraOffset.y +
localPlayer.transform.right * cameraOffset.x;
transform.position = Vector3.Lerp(transform.position, targetPosition, damping * Time.deltaTime);
}
}
I've tried changing the Script Execution Order, but nothing works. I don't know what's wrong.
Make sure you have a GameObject assigned to the LocalPlayer variable in your script. This object is looking in your hierarchy for something called 'cameraLookTarget' without quotes. Capitalization matters.
I recommend looking for a LocalPlayer object in your Awake() method and if it is null use Debug.Log("No local player assigned") to alert yourself that this is in fact not being assigned.
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.
using UnityEngine;
using System.Collections;
public class DayNight : MonoBehaviour{}
// Use this for initialization
void start (){
private float, smooth = 0.0000000000005;
}
// Update is called once per frame
void Update () {
IEnumerator IntensityChanging ()
{
float intensityA = 0.05f;
float intensityB = 5f;;
while( 1 == 1){
newIntensity = intensityA;
newIntensity = intensityB;
light.intensity = Math.Lerp(light.intensity, newIntensity, smooth * Time.deltaTime);
}
}
}
Errors
Assets/Scripts/DayNight.cs(7,15): error CS1525: Unexpected symbol private'
Assets/Scripts/DayNight.cs(6,14): error CS0116: A namespace can only contain types and namespace declarations
Assets/Scripts/DayNight.cs(13,39): error CS1525: Unexpected symbol(', expecting )',,', ;',[', or `='
Assets/Scripts/DayNight.cs(10,14): error CS0116: A namespace can only contain types and namespace declarations
Assets/Scripts/DayNight.cs(25,1): error CS8025: Parsing error
Thanks in advance.
All that code is just unusable, it seems a lot of copypaste's without no sense.
The most approximated I can think is:
using UnityEngine;
using System.Collections;
public class DayNight : MonoBehaviour{
private float smooth = 0.0000000000005;
// Use this for initialization
void start (){
}
float accumulate = 0;
// Update is called once per frame
void Update ()
{
float intensityA = 0.05f;
float intensityB = 5f;
accumulate += Time.deltaTime;
light.intensity = Math.Lerp(intensityA, intensityB, smooth * accumulate);
}
}
The only problem with that code is that light comes from nowhere, you need the reference to the light.