C# 如何访问Unity中其他场景中的游戏对象

C# 如何访问Unity中其他场景中的游戏对象,c#,unity3d,scene,gameobject,C#,Unity3d,Scene,Gameobject,我是Unity的初学者,所以 我想更改另一个场景中的游戏对象的精灵,这不是我正在使用的场景 这是第一个场景中的代码,我想把标记为“iman”的游戏对象放到第二个场景中 public class Gravidade : Move { void OnEnable () { rigid = GetComponent<Rigidbody2D> (); } // Use this for initialization void Start () { efeito.SetAct

我是Unity的初学者,所以

我想更改另一个场景中的游戏对象的精灵,这不是我正在使用的场景

这是第一个场景中的代码,我想把标记为“iman”的游戏对象放到第二个场景中

public class Gravidade : Move
{

void OnEnable ()
{
    rigid = GetComponent<Rigidbody2D> ();
}
// Use this for initialization
void Start ()
{
    efeito.SetActive (false);
    iman_pos = GameObject.FindGameObjectWithTag ("iman").transform.position;
    imanbase_pos = GameObject.FindGameObjectWithTag ("imanbase").transform.position;

    targetRotation = transform.rotation;
    vidas = 3;

    coins = 0;
    //vidas_text = GetComponent<Text>();

    //GUIText vidas_text = GameObject.FindWithTag("vidas").GetComponent<GUIText>() as GUIText;

    rend = GetComponent<Renderer> ();
}

// Update is called once per frame

void Update ()
{

    backgrounds = GameObject.FindGameObjectsWithTag ("background");
    obstaculos = GameObject.FindGameObjectsWithTag ("obstaculos");
    allcoins = GameObject.FindGameObjectsWithTag ("coins");
    allvidas = GameObject.FindGameObjectsWithTag ("vidas123");
    powerups = GameObject.FindGameObjectsWithTag ("powerup");



    float dist = 1 / (iman_pos.y - imanbase_pos.y - 2);


    bool space = Input.GetKeyUp ("space");



    if (/*Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended */ space && vidas > 0 && lose == false) {
        PlaySound (0);
        magnetismo = !magnetismo;
        targetRotation *= Quaternion.AngleAxis (180, Vector2.left);
        lose_text.text = "";
    }


    transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, 10f * 0.5f * Time.deltaTime); 


    if (magnetismo) {
        velocity += gravityModifier * Physics2D.gravity * Time.deltaTime - (dist * dist * Physics2D.gravity * amplitudeOsc);
        rigid.velocity = velocity;
        //nbTouches = 0;

    } else {
        velocity += gravityModifier * Physics2D.gravity * Time.deltaTime;
        rigid.velocity = velocity;
        //nbTouches = 0;
    }



    if (Mathf.Abs (velocity.y) > maxVelocity) {
        velocity.Normalize (); 
        velocity = velocity * maxVelocity;
    }

    //Debug.Log(velocity);

    if (iman_pos.y + 2.5f < imanbase_pos.y + 0.8f)
        iman_pos.y = imanbase_pos.y;

    if (iman_pos.y + 2.5f > tecto.y + 2.5f)
        iman_pos.y = imanbase_pos.y;

    //Time.timeScale = 0;

    //rigid.velocity = velocity;
    //Debug.Log (dist);
}



/*private void OnCollisionEnter2D(Collision2D collision)
{
    //Debug.Log ("Perdeste!!");
    //Time.timeScale = 0;
}*/

void OnTriggerEnter2D (Collider2D other)
{
    if (other.tag == "coins") {
        PlaySound (3);
        Destroy (other.gameObject);
        coins = coins + 1;
        coins_text.text = "x" + coins;

    }

    if (other.tag == "obstaculos" && bater == true) {
        //Destroy (other.gameObject);
        if (vidas > 0) {
            PlaySound (1);
            vidas = vidas - 1;
            vidas_text.text = "x" + vidas;
            //Debug.Log (vidas);
            Destroy (other.gameObject);
            BlinkPlayer (3);
            if (vidas == 0) {
                PlaySound (2);
                Time.timeScale = 0;
                losemenu.SetActive (true);
                //SceneManager.LoadScene (0);
            }
        }
    }

    if (other.tag == "imanbase" || other.tag == "tecto") {
        lose = !lose;
        PlaySound (2);
        Time.timeScale = 0;
        losemenu.SetActive (true);
        //SceneManager.LoadScene (0);
    }

    if (other.tag == "vidas123") {
        PlaySound (4);
        vidas = vidas + 1;
        vidas_text.text = "x" + vidas;
        Destroy (other.gameObject);
    }

    if (other.tag == "passar nivel") {
        Time.timeScale = 0;
        lose_text.text = "Victory!";
        //SceneManager.LoadScene (0);
    }
    if (other.tag == "powerup") {
        Destroy (other.gameObject);
        StartCoroutine (pw ());
    }


}

void PlaySound (int clip)
{
    GetComponent<AudioSource> ().clip = audioClip [clip];
    GetComponent<AudioSource> ().Play ();
}

void BlinkPlayer (int numBlinks)
{
    StartCoroutine (DoBlinks (numBlinks, 0.2f));
}

IEnumerator DoBlinks (int numBlinks, float seconds)
{
    for (int i = 0; i < numBlinks * 2; i++) {
        rend.enabled = !rend.enabled;
        yield return new WaitForSeconds (seconds);
    }

    rend.enabled = true;
}


IEnumerator pw ()
{

    float timePassed = 0;
    while (timePassed < 3) {
        efeito.SetActive (true);
        bater = false;
        foreach (GameObject back in backgrounds) {
            back.GetComponent<Move> ().speed = 15.0f;
        }
        foreach (GameObject obs in obstaculos) {
            obs.GetComponent<Move> ().speed = 15.0f;
        }
        foreach (GameObject ac in allcoins) {
            ac.GetComponent<Move> ().speed = 15.0f;
        }
        foreach (GameObject vd in allvidas) {
            vd.GetComponent<Move> ().speed = 15.0f;
        }
        foreach (GameObject pu in powerups) {
            pu.GetComponent<Move> ().speed = 15.0f;
        }

        timePassed += Time.deltaTime;
        //Debug.Log (timePassed);
        yield return null;
    }
    efeito.SetActive (false);
    bater = true;
    foreach (GameObject back in backgrounds) {
        back.GetComponent<Move> ().speed = 3.0f;
    }
    foreach (GameObject obs in obstaculos) {
        obs.GetComponent<Move> ().speed = 3.0f;
    }
    foreach (GameObject ac in allcoins) {
        ac.GetComponent<Move> ().speed = 3.0f;
    }
    foreach (GameObject vd in allvidas) {
        vd.GetComponent<Move> ().speed = 3.0f;
    }
    foreach (GameObject pu in powerups) {
        pu.GetComponent<Move> ().speed = 3.0f;
    }
}}
公共类孕妇:移动
{
void-OnEnable()
{
刚性=GetComponent();
}
//用于初始化
无效开始()
{
efeito.SetActive(假);
iman_pos=GameObject.FindGameObjectWithTag(“iman”).transform.position;
imanbase_pos=GameObject.FindGameObjectWithTag(“imanbase”).transform.position;
targetRotation=变换旋转;
vidas=3;
硬币=0;
//vidas_text=GetComponent();
//GUIText vidas_text=GameObject.FindWithTag(“vidas”).GetComponent()作为GUIText;
rend=GetComponent();
}
//每帧调用一次更新
无效更新()
{
backgrounds=GameObject.FindGameObjectsWithTag(“背景”);
obstaculos=GameObject.FindGameObjectsWithTag(“obstaculos”);
allcoins=GameObject.FindGameObjectsWithTag(“硬币”);
allvidas=GameObject.FindGameObjectsWithTag(“vidas123”);
powerups=GameObject.FindGameObjectsWithTag(“powerup”);
浮动距离=1/(iman_位置y-imanbase_位置y-2);
bool space=Input.GetKeyUp(“空格”);
如果(/*Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Ended*/space&&vidas>0&&lose==false){
播放声音(0);
magnetismo=!magnetismo;
targetRotation*=四元数角度轴(180,向量2.左);
lose_text.text=“”;
}
transform.rotation=Quaternion.Lerp(transform.rotation,targetRotation,10f*0.5f*Time.deltaTime);
if(磁学){
速度+=重力修改器*Physics2D.gravity*Time.deltaTime-(距离*dist*Physics2D.gravity*amplitudeOsc);
刚性。速度=速度;
//nbc=0;
}否则{
速度+=重力修改器*Physics2D.gravity*时间.deltaTime;
刚性。速度=速度;
//nbc=0;
}
如果(Mathf.Abs(velocity.y)>maxVelocity){
Normalize();
速度=速度*最大速度;
}
//Log(速度);
如果(iman_位置y+2.5f检测y+2.5f)
iman_pos.y=imanbase_pos.y;
//Time.timeScale=0;
//刚性。速度=速度;
//调试日志(dist);
}
/*专用空心OnCollisionInter2D(碰撞2D碰撞)
{
//Log(“Perdeste!!”);
//Time.timeScale=0;
}*/
无效OnTiggerEnter2D(碰撞的R2D其他)
{
如果(other.tag==“coins”){
播放声音(3);
销毁(其他游戏对象);
硬币=硬币+1;
coins_text.text=“x”+硬币;
}
if(other.tag==“obstaculos”&&bater==true){
//销毁(其他游戏对象);
如果(视频>0){
播放声音(1);
vidas=vidas-1;
vidas_text.text=“x”+vidas;
//调试日志(vidas);
销毁(其他游戏对象);
闪烁播放器(3);
如果(vidas==0){
播放声音(2);
Time.timeScale=0;
losemenu.SetActive(true);
//SceneManager.LoadScene(0);
}
}
}
if(other.tag==“imanbase”| | other.tag==“tecto”){
输=!输;
播放声音(2);
Time.timeScale=0;
losemenu.SetActive(true);
//SceneManager.LoadScene(0);
}
如果(other.tag==“vidas123”){
播放声音(4);
vidas=vidas+1;
vidas_text.text=“x”+vidas;
销毁(其他游戏对象);
}
如果(other.tag==“passar nivel”){
Time.timeScale=0;
lose_text.text=“胜利!”;
//SceneManager.LoadScene(0);
}
如果(other.tag==“通电”){
销毁(其他游戏对象);
启动程序(pw());
}
}
无效播放声音(int剪辑)
{
GetComponent().clip=audioClip[clip];
GetComponent().Play();
}
无效闪烁播放器(整数链接)
{
start例程(多链接(numBlinks,0.2f));
}
IEnumerator链接(int numBlinks,float seconds)
{
对于(int i=0;i
这是第二个场景中第二个脚本的代码,我想在这里更改精灵

public class MainMenu : MonoBehaviour
{
public GameObject menustore;
public Sprite skinteste;


public void PlayGame ()
{
    SceneManager.LoadScene (1);
    Time.timeScale = 1f;
}
public void Home1(){
    SceneManager.LoadScene (0); 
}

public void openstore ()
{
    menustore.SetActive (true);
}

public void skin1 ()
{
    //DontDestroyOnLoad (GameObject.FindWithTag("iman"));
    GameObject.FindWithTag("iman").GetComponent<SpriteRenderer>().sprite 
= skinteste;
}}
public类主菜单:MonoBehavior
{
公共游戏对象菜单;
公共雪碧;
公共虚拟游戏()
{
SceneManager.LoadScene(1);
Time.timeScale=1f;
}
公共房屋1(){
Sc
public class YourClass : MonoBehaviour {

public static YourClass singleton = null;

void Awake()
{
     DontDestroyOnLoad(this.gameObject);
     if(singleton == null)
     {
         singleton = this;
     } 
}}