Unity-trying to adjust brightness - c#

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.

Related

Got 2 errors with 'start' and 'update' error CS0111: Type 'Enemy' already defines a member called 'Start' with the same parameter types how can i fix?

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.

Making a 5 second timer for a prefab

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

C# Pong Game - problem with ball scripting

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)"

ThirdPersonMovement script Unity

I was following this video: https://youtu.be/4HpC--2iowE?t=686
And after making the thirdpersonmovement script, I get an error that he did not get. Can anyone help me? There's probably a very small error that I cannot see, I must be blind. I followed the exact steps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 6f;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vetrical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
controller.Move(direction * speed * Time.deltaTime);
}
}
}
Error:
Assets\Scripts\ThirdPersonMovement.cs(16,57): error CS0103: The name 'Vertical' does not exist in the current context
Says error is on line 16. where "vector3 direction" is located
If the code is kinda wonky sorry, first time using this, was a weird set up for me.Thank you in advance.
There is a typo:
vetrical
vertical
t and r are swapped

'Vector3' does not contain a definition for 'Input' error

"Assets/MovePlayer.cs(27,70): error CS1061: 'Vector3' does not contain a definition for 'Input' and no accessible extension method 'Input' accepting a first argument of type 'Vector3' could be found (are you missing a using directive or an assembly reference?)"
I am making a small game, I've been trying to add camera relative movements and it keeps showing me the error above.
Here is my code for reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovePlayer : MonoBehaviour
{
public Transform cam;
Vector2 input;
void Update()
{
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
input = Vector2.ClampMagnitude(input,1);
Vector3 camF = cam.forward;
Vector3 camR = cam.right;
camF.y = 0;
camR.y= 0;
camF = camF.normalized;
camR = camR.normalized;
transform.position += (camF*input.y + camR.input.x)*Time.deltaTime*5;
}
}
I've also added a CameraLook component to my Main Camera, if you would like to look at that as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraLook : MonoBehaviour {
Vector2 rotation = new Vector2 (0, 0);
public float speed = 3;
void Update () {
rotation.y += Input.GetAxis ("Mouse X");
rotation.x += -Input.GetAxis ("Mouse Y");
transform.eulerAngles = (Vector2)rotation * speed;
}
}
This likely needs to be transform.position += (camF*input.y + camR * input.x)*Time.deltaTime*5;
You're trying to access camR.input which isn't a property of camR which is a Vector3. I think you're intending to multiply camF by input.x instead.
FYI, You can double click on the error message in visual studio, or monodevelop and it will take you to the error line.
You have camR.input.x, which means it's trying to access a member of camR (which is a Vector3) called input, which doesn't exist.
You meant to write camR * input.x.

Categories