How Can I Change The Sprite of A Child
Hi, and thank you in advance for helping me with this, it's my first Unity question. I am currently attempting to assemble a system in which a character is represented by 2 sprites, a head and a torso, which are swapped out based on variables (right now with teams to keep things as simple as possible).
I figured that the easiest way to do this would be to create 2 child game objects (named head and torso). I've been attempting to figure out how to change the sprite of a child object based on variables assigned in a spawn script.
Here is the section of the spawn script where I set the variable:
public class Spawn : MonoBehaviour
{
public int Team = 1;
[SerializeField] private Boolean incapacitated = false;
[SerializeField] private Boolean alive = true;
}
And here is the script to draw the torso as one of two options (a neutral option and a blue option). The code is a bit of a mess due to about 3 hours of tinkering + searching the forum and not finding results, but hopefully someone will be able to point me in the right direction:
public class DrawTorso : MonoBehaviour
{
private SpriteRenderer sprites = this.transform.GetComponentsInChildren<SpriteRenderer>(); //which torso is selected
private Sprite body_neutral;
private Sprite body_blue;
private SpriteRenderer spriteRender;
// Update is called once per frame
void Update()
{
if(team == 0)
{
if (this.transform.Find("Torso"))
{
this.transform.GetComponentsInChildren<SpriteRenderer> = body_neutral;
Torso.SpriteRenderer.sprite = body_neutral;
Debug.Log("neutral torso");
}
}
if (team == 1)
{
if (this.transform.Find("Torso"))
{
this.transform.Find("Torso").GetComponents<SpriteRenderer>();
Debug.Log("blue torso");
}
}
else //if for some reason this person doesn't have a team throw them into neutral
{
if (this.transform.Find("Torso"))
{
this.transform.Find("Torso").GetComponents<SpriteRenderer>();
Debug.Log("neutral torso");
}
}
}
}
Answer by leftistthot · Jul 21, 2020 at 02:14 AM
Hi everyone. After a long process of tinkering and working with a friend, I've figured out the solution. I figured I'd throw it here in-case anyone else wants to change the sprite of a child object in the future.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Resources;
using UnityEngine;
using UnityEngine.UI;
public class DrawTorso : MonoBehaviour
{
private int Team;
[SerializeField] private Sprite body_neutral;
[SerializeField] private Sprite body_blue;
public SpriteRenderer torsoSprite; //which torso is selected
void Start()
{
torsoSprite = this.transform.Find("Torso").GetComponent<SpriteRenderer>();
Team = this.transform.GetComponent<Spawn>().Team;
}
// Update is called once per frame
void Update()
{
Team = this.transform.GetComponent<Spawn>().Team;
if (Team == 0)
{
torsoSprite.sprite = body_neutral;
Debug.Log("neutral torso 0");
}
if (Team == 1)
{
torsoSprite.sprite = body_blue;
Debug.Log("blue torso 1");
}
else //if for some reason this person doesn't have a team throw them into neutral
{
torsoSprite.sprite = body_neutral;
Debug.Log("exception torso, value of team is:" + Team);
}
}
}
Your answer
Follow this Question
Related Questions
assigning sprites to game object via script 0 Answers
Problem Changing Sprites with an Array of Sprites 1 Answer
Get sprite size and position 0 Answers
How can I cast shadows onto sprites? 0 Answers