我试图改变场景中玩家的精灵。 我有一个脚本来管理从播放器切换到另一个播放器时激活的脚本。 我想添加一个功能,当切换非活动播放器时,它会变为黑色&白色的精灵和积极的球员保持着色的精灵。 任何人都可以帮助,因为首先不改变玩家的精灵和玩家的第二个游戏对象吗?
以下是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class switchCharacter : MonoBehaviour {
public GameObject Psyco1;
public GameObject Psyco2;
public PlayerControllerPsyco1 script1;
public PlayerControllerPsyco2 script2;
public Sprite sprite1; // Psyco1 color
public Sprite sprite2; // Psyco2 decolorized
public SpriteRenderer spriteRendererPsyco1;
public SpriteRenderer spriteRendererPsyco2;
void Start (){
script1 = Psyco1.GetComponent<PlayerControllerPsyco1>();
script2 = Psyco2.GetComponent<PlayerControllerPsyco2>();
script1.enabled = true;
script2.enabled = false;
spriteRendererPsyco1 = GetComponent<SpriteRenderer>();
spriteRendererPsyco2 = GetComponent<SpriteRenderer>();
if (spriteRendererPsyco1.sprite == null) // if the sprite on spriteRenderer is null then
spriteRendererPsyco1.sprite = sprite1; // set the sprite to Psyco1 Color
}
void Update (){
if (Input.GetButtonDown("switch1")){
script1.enabled = !script1.enabled;
script2.enabled = !script2.enabled;
//spriteRendererPsyco1.sprite = sprite2;
DesaturizePsyco (); // call method to change Psyco sprite
}
}
public void changeCharacterBtn() {
if (script1.enabled = !script1.enabled) // if the spriteRenderer sprite = sprite1 then change to sprite2
{
GameObject.Find ("PlayerFirst").GetComponent<SpriteRenderer> ().sprite = sprite2;
}
//script1.enabled = !script1.enabled;
if (script2.enabled = !script2.enabled;) // if the spriteRenderer sprite = sprite1 then change to sprite2
{
GameObject.Find ("PlayerSecond").GetComponent<SpriteRenderer> ().sprite = sprite1;
}
//script2.enabled = !script2.enabled;
DesaturizePsyco ();
}
void DesaturizePsyco ()
{
if (spriteRendererPsyco1.sprite == sprite1) // if the spriteRenderer sprite = sprite1 then change to sprite2
{
GameObject.Find ("PlayerFirst").GetComponent<SpriteRenderer> ().sprite = sprite2;
}
else
{
spriteRendererPsyco1.sprite = sprite1; // otherwise change it back to sprite1
}
}
}
答案 0 :(得分:0)
首先尝试始终发布最小的剥离脚本:此处changeCharacterBtn()
方法无效(我建议您对帖子进行编辑)。在if (script2.enabled = !script2.enabled;)
还有一个拼写错误。另外我猜你的代码在播放时会抛出错误:你检查了控制台吗?
关于您的脚本,以下是我看到的错误:
spriteRendererPsyco1 = GetComponent<SpriteRenderer>();
代替spriteRendererPsyco1 = Psyco1.GetComponent<SpriteRenderer>();
(Psyco2相同)script1
,script2
,spriteRendererPsyco1
和spriteRendererPsyco2
应设置为私有,因为您在Start()
中引用它们(不需要它们出现在检查员)所以这是您的脚本的更正版本:
public class switchCharacter : MonoBehaviour
{
public GameObject Psyco1;
public GameObject Psyco2;
private PlayerControllerPsyco1 script1;
private PlayerControllerPsyco1 script2;
public Sprite sprite1; // Psyco1 color
public Sprite sprite2; // Psyco2 decolorized
private SpriteRenderer spriteRendererPsyco1;
private SpriteRenderer spriteRendererPsyco2;
void Start()
{
script1 = Psyco1.GetComponent<PlayerControllerPsyco1>();
script2 = Psyco2.GetComponent<PlayerControllerPsyco1>();
script1.enabled = true;
script2.enabled = false;
spriteRendererPsyco1 = Psyco1.GetComponent<SpriteRenderer>();
spriteRendererPsyco2 = Psyco2.GetComponent<SpriteRenderer>();
if(spriteRendererPsyco1.sprite == null) // if the sprite on spriteRenderer is null then
spriteRendererPsyco1.sprite = sprite1; // set the sprite to Psyco1 Color
}
void Update()
{
if(Input.GetButtonDown("switch1"))
{
script1.enabled = !script1.enabled;
script2.enabled = !script2.enabled;
//spriteRendererPsyco1.sprite = sprite2;
DesaturizePsyco(); // call method to change Psyco sprite
}
}
void DesaturizePsyco()
{
if(spriteRendererPsyco1.sprite == sprite1) // if the spriteRenderer sprite = sprite1 then change to sprite2
{
GameObject.Find("PlayerFirst").GetComponent<SpriteRenderer>().sprite = sprite2;
}
else
{
spriteRendererPsyco1.sprite = sprite1; // otherwise change it back to sprite1
}
}
}
作为旁注,这里有一些关于通用编程的建议:
bool
变量)Psyco1
但script1
,...)希望这有帮助,