Home>
I want to solve
I want to be able to assign a value to newColor.
problemError statement
cs (58,25): error: CS0165: Use of unassigned local variable'newColor'
Line 58: ps.startColor = newColor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class ParticleManager: MonoBehaviour
{
ParticleSystem ps;
void Start ()
{
ps = GetComponent<ParticleSystem>();
}
void Update ()
{
if (Input.GetKey (KeyCode.Space))
{
PlayFirework ();
}
}
void PlayFirework ()
{
var rand = Random.Range (0, 2);
var r2 = Random.Range (0, 256);// red random
var g2 = Random.Range (0, 256);// green random
var b2 = Random.Range (0, 256);// blue random
We received advice on/* Random and made changes.
Random.Range does not include the max value in the result, so if I want to make it random from 0 to 255, it will be Random.Range (0, 256).
* /
Color32 newColor;
// Red is fixed at 255 and green and blue are random at 0-255
if (rand == 0)
{
newColor = new Color32 (255, (byte) g2, (byte) b2, 255);
}
// Green is fixed at 255 and red and blue are random
// Blue is fixed at 255 and red and green are random
ps.startColor = newColor;//
ps.Play ();
}
}
What I tried
When the if statement was commented and only newColor = new Color32 () was extracted and executed, it could be executed without error.
Therefore, it is possible that the if statement is incorrect, or that the value in the if statement is not reflected in the C # writing style.
/*
// Red is fixed at 255 and green and blue are random at 0-255
if (rand == 0)
{
newColor = new Color32 (255, (byte) g2, (byte) b2, 255);
}
// Green is fixed at 255 and red and blue are random at 0-255
// Blue is fixed at 255 and red and green are random at 0-255
* /
newColor = new Color32 (255, (byte) g2, (byte) b2, 255);
Environment i am using
Unity2020.1.0f1 Personal
Visual studio 2019
-
Answer # 1
Related articles
- how to find the maximum value using for statement and if statement in python
- c # - i set an excel value using npoi, but it is not reflected in the reference
- c # - jumping when using rigidbody2dvelocity
- i want to download a file from a bucket using a uri in c # with a format starting with s3: //
- c # - [wpf] how to put a changing value in the binding path
- c # syntax when using azure cognitive service
- c # - i want to make a server-side gui application using websocket-sharp
- javascript - i want to shorten the code using a for statement because it is long
- how to avoid using console for interprocess communication in c # wcf ipc?
- [nodejs] i want to express a mysql statement using a for statement and variables
- javascript - get the value of the array in the object , how to use in an if statement
- i want to get the characters in a word file in c # without using visual studio
- javascript - behavior of if statement using or condition
- c # - how to move an object straight using unity3d dotween
- c # - large number of same errors (using vuforia)
- c # - blackjack (creating a deck using a mold you made)
- c # - i get an error in the if statement
- php - about passing value to form using tag
- c # - i want to run unity: transformtranslate using rigidbody
Related questions
- c # - in a script? error cs1056: unexpected character'?' is displayed even though there is no
- candidates are not displayed even if i hit a period in c # opened in unity
- c # - the player character of unity rotates
- c # - touch judgment of unity object
- c # - i don't understand the code below
- c # - i want to gradually expand the screen of the winning player once the victory or defeat is decided
- c # - i want to specify a different url from the script side for the video player component of unity
- c # - conversion from enum type to enum type
- c # - [unity] an unintended transition occurs in the state transition of animator controller
The if statement itself is correct, but I think the problem is that if the value of rand is non-zero, the variable newColor is left unassigned.
The variable newColor is just before the if statement
Color32 newColor;
Was just declared, but not initialized.So when declaring a variable
If you initialize it or add else to the if statement as shown below, the error will be resolved without commenting out the if statement.