简体   繁体   中英

How do I call an object from another script, trying to put in the SetActive function and it doesnt like "static", VERY NEW to Unity/C#

I am trying to reference my speechBubbletTOSpawn object that was in my SpeechAppearsSETACTIVE script, but the only way I know how to do this is to make the speechBubbletTOSpawn object in the SpeechAppearsSETACTIVE code a "public static" variable.

Is there another way to do this?

When I do it this way I get the following error:

Assets/RunOnce.cs(8,37): error CS0120: An object reference is required for the non-static field method, or property 'SpeechAppearsSETACTIVE.speechBubbleTOSpawn'

I am trying to make a speech bubble appear when Torso 2 is selected, and this happens in the SpeechAppearsSETACTIVE script. The current script below is supposed to check if this has already been done ONCE, and if it has, to not make the speech bubble appear on the second instance of Torso 2 being selected.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RunOnce : MonoBehaviour
{
    int FirstTime; //boolean true/false

    private GameObject YesSpeech => SpeechApearsSETACTIVE.speechBubbleTOSpawn;

    // Start is called before the first frame update
    void Start()
    {
    }
    
    // Update is called once per frame
    void Update()
    {
        FirstTime = PlayerPrefs.GetInt("FirstTime"); //obtains int value
                                                     // 0 = true
                                                     // 1 = false
    
        if (FirstTime == 0)
        {
            YesSpeech.SetActive(true);
        }
        else
        {
            YesSpeech.SetActive(false);
        }
    }
}

The error says it all, you need to create a reference for your script SpeechApearsSETACTIVE in order to have access to it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RunOnce : MonoBehaviour
{
    int FirstTime; //boolean true/false

    public SpeechApearsSETACTIVE speechApearsSETACTIVE;
    private GameObject YesSpeech;

    // Start is called before the first frame update
    void Start()
    {
        YesSpeech = speechApearsSETACTIVE.speechBubbleTOSpawn;
    }

    // Update is called once per frame
    void Update()
    {
        FirstTime = PlayerPrefs.GetInt("FirstTime"); //obtains int value
                                                 // 0 = true
                                                 // 1 = false

        if (FirstTime == 0)
        {
            YesSpeech.SetActive(true);
        }
        else
        {
            YesSpeech.SetActive(false);
        }
    }
}

Now you can simply just drag and drop the gameObject that the SpeechApearsSETACTIVE script is attached to it through Unity's Editor.

If your SpeechApearsSETACTIVE is not attached anywhere, then you should create an empty gameObject and just add the script as a component and use that one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM