SceneChange script compiler error: unable to access due to protection level - c#

I have this script that allows me to change to a different scene when I push a button. I've used it multiple times before with no issue, but recently I've been getting this compiler error that's telling me the script can't be accessed due to its protect level. I did some research and it supposedly means something is set to private instead of public, but everything in my script is public to begin with. Please help? According to Unity, the error is the ".LoadScene" inside public void DoSceneChange().
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChange : MonoBehaviour
{
[SerializeField]
string levelToLoad;
public void DoSceneChange()
{
SceneManager.LoadScene(levelToLoad);
}
}

Related

GameObject.SendMessage not working, how can i fix it?

I'm trying to do a script that when the player pass by a item, it activates it's effect in the player.
But this error apears:
This is my code:
`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Bonus : MonoBehaviour
{
public string name;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Player"))
{
collision.SendMessage(name, null, SendMessageOptions.RequireReceiver);
Destroy(gameObject);
}
}
}
`
I already tried adding the
SendMessageOptions.RequireReceiver
but it didn't work
What the error is trying to tell you is:
On your collision's GameObject there is no component attached that implements a method called whatever the value of name is (in your case doubleSpeed).
By default the option already is SendMessageOptions.RequireReceiver, if there is none you get that error message.
The receiving method can choose to ignore the argument by having zero parameters. If options is set to SendMessageOptions.RequireReceiver an error is printed if the message is not picked up by any component.
If you want to ignore the case where there is no receiver you would rather pass in SendMessageOptions.DontRequireReceiver
In general instead of using SendMessage at all rather get your component and call the method directly

Unity: Calling a method from another script

I am completely new to Unity and all other answers I've found for this go over my head.
So far I've run everything from the same script, which is getting very big and messy. Therefore I am trying to learn how to call methods from other scripts.
I have a dropdown menu with the code in one script and I'm trying to call that code from another.
ScriptA:
using UnityEngine;
public class ChoseLanguage: MonoBehaviour
{
public TMPro.TMP_Dropdown myDrop;
DisplayController displayController;
public void DropdownChooseLanguage()
{
if (myDrop.value == 1)
PlayerPrefs.SetString("chosenLanguage", "Spanish");
if (myDrop.value == 2)
PlayerPrefs.SetString("chosenLanguage", "Japanese");
if (myDrop.value == 3)
PlayerPrefs.SetString("chosenLanguage", "Korean");
if (myDrop.value == 4)
PlayerPrefs.SetString("chosenLanguage", "Icelandic");
Debug.Log(PlayerPrefs.GetString("chosenLanguage"));
displayController.DropdownSetLanguage();
}
}
The selection code works by itself, and the debug.Log shows that the chosen language is being saved correctly to PlayerPrefs.
The error comes when it tries to read the "displayController.DropdownChooseLanguage();" line. (Line 28)
Unity gives this error:
NullReferenceException: Object reference not set to an instance of an object
ChoseLanguage.DropdownChooseLanguage () (at Assets/Scripts/ChoseLanguage.cs:28)
Script B
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using TMPro;
public class DisplayController : MonoBehaviour
{
...
public void DropdownSetLanguage()
{
SetFileName();
setLanguage.gameObject.SetActive(false);
Start();
}
...
}
Earlier, the exact same code from Script A was placed in ScriptB and all the code worked as it should.
This is a very noob question but I have simply just never been able to understand how exactly to access other scripts correctly.
Any help will be greatly appreciated.
Thanks.
EDIT:
I found a solution to this but I'll keep the question up in case other beginners have the same problem or if anyone has a better solution.
I made the DisplayController displayController; into public DisplayController displayController; and then dragged the gameobject with the displaycontroller script attached into the slot for it.
DropdownSetLanguage is public and it seems you think you can access it by
creating a DisplayController object named displayController.
It is ok
about to access a class but, displayController should be null in this state.
Because you have to assign an initial DisplayController to displayController
in your first code.
You can add public or [Serializable] tag in front of
DisplayController displayController; in your first code then, you can add
a GameObject which has a DisplayController script in it in Unity editor.
accessibility

Unable to configure saving and loading with firebase

I followed all steps such as adding the database SDK to Unity etc., but when I try to connect Unity to Firebase, I'm getting an error.
Assets\Script\RealTimeLoading.cs(14,21): error CS0029: Cannot implicitly convert type 'Firebase.Database.DatabaseReference' to 'DatabaseReference'
I followed a lot of tutorials, they are never getting errors, just me. Here is my code
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RealTimeLoading : MonoBehaviour
{
DatabaseReference reference;
// Start is called before the first frame update
void Start()
{
reference = FirebaseDatabase.DefaultInstance.RootReference;
}
// Update is called once per frame
void Update()
{
}
}
The error is on this line:
FirebaseDatabase.DefaultInstance.RootReference;
What did I do wrong?
After briefly going through your using statements, I don't believe that there should be a DatabaseReference other than the one in Firebase.Database. That makes me think that you have another class in your project's root namespace with the same name.
First, I'd recommend removing Firebase.Unity.Editor and definitely let me know if there's still some documentation recommending it.
Then you should be able to simply write:
using DatabaseReference = Firebase.Database.DatabaseReference;
with your other using directives. So the top of your file may look like:
using Firebase;
using Firebase.Database;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DatabaseReference = Firebase.Database.DatabaseReference;
Be careful with this because if there is another class named DatabaseReference in your code base, you may run into this naming conflict more often. It could be beneficial to either move it to its own namespace or to rename it if possible. But this should get you unstuck immediately.
Alternatively: instead of adding the using alias, you may instead fully qualify the name in your class. For example:
using Firebase;
using Firebase.Database;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RealTimeLoading : MonoBehaviour
{
Firebase.Database.DatabaseReference reference;
// Start is called before the first frame update
void Start()
{
reference = FirebaseDatabase.DefaultInstance.RootReference;
}
}
I would recommend against doing both of these in one file, but you may have to use each under different circumstances.

Setting up a UI slider to control post processing effects?

As the title says, I'm trying to set up a UI slider so the player can adjust some of the post processing settings (specifically the exposure and temperature) while the game is running.
To bring you up to speed:
I'm using version one of the post processing stack, as found here:
https://assetstore.unity.com/packages/essentials/post-processing-stack-83912
I'm very new to C#; I'm at the "Frankenstein" stage of learning how to code (following tutorials and modifying what works until it breaks or does what I'm trying to accomplish).
I figure my best shot is to try to adapt what I learned from this tutorial on creating an audio volume slider: https://www.youtube.com/watch?v=YOaYQrN1oYQ&t=122s
This is the code I've cobbled together so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BrightnessSlider : MonoBehaviour {
public void SetBrightness (float brightness)
{
Debug.Log(brightness);
}
}
Specific issues I'm having at specific points in the tutorial:
2:07 For the function of the slider, the tutorial sets up a dynamic float that matches the custom method (SetVolume) they specify. When I try setting up my own function with a custom method (SetBrightness), I can't find it. I'm also not sure if I need to set a different object instead of the canvas for this step.
3:47 In the tutorial they expose a parameter for the volume so that it can be manipulated through script, but I don't know what the equivalent of that would be for post processing.
For the record, I was able to follow this tutorial to create my own audio slider and got it working with no problems.
One last thing: I opened up the script for the post processing profile and found the variable type I think I'd need or would at least be somewhat relevant: ColorGradingModel, but I honestly have no idea what to do with this information.
Update July 09, 2018


I've since been looking over #Nol's code and had someone else look at it and help me out with it. At the moment, the slider's functionality(not sure if that's the correct terminology but that's what I've been sticking with) is set up through the On Value Changed field in the inspector , but it's not actually driving/changing the brightness values. I had someone else (who's far more qualified than I am) look at it with me. It seems like it should work the way they've set it up, but something is getting lost in translation between the method and the slider.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;
using UnityEngine.UI;
public class BrightnessSlider : MonoBehaviour
{
public Slider slider;
public PostProcessingProfile Default;
private ColorGradingModel cgm;
private void Start()
{
//I haven't been able to get this to not return some sort of error,
//and I'm not even sure of its usefulness.
//I've been keeping it commented out for the most part.
Default.profile.TryGetSettings(out cgm);
}
public void SetBrightness(float brightness)
{
ColorGradingModel.Settings settings = cgm.settings;
settings.basic.postExposure = brightness;
cgm.settings = settings;
Debug.Log("Brightness is: " + brightness); //For testing purposes
}
}
It seems like you have the core of what you need to know for changing your settings down. That's good.
You'll need a few simple changes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing; //How you'll access PPV (Post Processing Volume) models and settings
public class BrightnessSlider : MonoBehaviour {
PostProcessingVolume ppv; //You can make this public to set in inspector
ColorGradingModel cgm; //can use ppv.profile.TryGetSettings(out cgm) in Start()
public void SetBrightness (float brightness)
{
cg.[setting you want to change].value = brightness;
}
}

AdEventListener does not exist in the type StartApp.StartAppWrapper

I am getting the following error in unity , I tried to refresh the project and to see any suggestion solution but still the same error
Assets/Plugins/Scripts/AdsManager.cs(10,43): error CS0426: The nested
type `AdEventListener' does not exist in the type
StartApp.StartAppWrapper
Here is the code:
using UnityEngine;
using GoogleMobileAds.Api;
using ChartboostSDK;
using System;
using StartApp;
using UnityEngine.Advertisements;
using SATestAds;
using UnityEngine;
using GoogleMobileAds.Api;
using ChartboostSDK;
using System;
using StartApp;
using UnityEngine.Advertisements;
using SATestAds;
public class AdsManager : StartAppWrapper.AdEventListener
{
private bool testMode = false;
private bool loggerEnabled = false;
private float delay = 0f;
}
AdEventListener is an interface which requires the following conditions be met before you can use it:
1.That your Unity version is Unity 4.2 and above.
2.That your current Platform is set to Android. Go to File --> Build Settings..., select Android and click the Switch Platform button. This is likely the issue.
Both of these checks are being done with Unity's preprocessor directives such as UNITY_ANDROID and UNITY_4_1. That interface is only declared when both are true.
Note that this answer assumes that you have already imported the StartApp-SDK. If you have not, you can get that here.

Categories