- Home /
Question by
Daeridanii · Dec 29, 2017 at 04:18 AM ·
c#list
Change Variable Value by List Number
Hello all. I'm trying to change the values of variables based on whether a certain sprite in a list is being used in the Sprite Renderer:
using UnityEngine;
using System.Collections.Generic;
public class Answers : MonoBehavior
{
public List<Sprite> exampleList = new List<Sprite>();
public float valueToChange;
public int ID;
private Rigidbody2D rb;
private SpriteRenderer sr;
void Start()
{
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
exampleList[ID] = sr.sprite;
}
void FixedUpdate();
{
if (sr.sprite = exampleList[0])
valueToChange = 3.2f;
rb.mass = 1.5f;
if (sr.sprite = exampleList[1])
valueToChange = 6.1f;
rb.mass = 0.3f;
//... and so on
}
}
The issue I'm having is that this always returns the last sprite in the list and uses its values, regardless of what I set the ID value to. I'm still very much a beginner so it might be that I'm just overlooking something. Thanks!
Comment
Best Answer
Answer by Larry-Dietz · Dec 29, 2017 at 04:03 PM
try changing your if statements to use two equals signs. A single equals sign is an assignment, two equals signs are a comparison.
i.e. if (sr.sprite == exampleList[0])
-Larry
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer