Can anyone help me with the loadingSceneIndex issue - c#

does anyone know where the error is? I've been looking for various solutions but I didn't get what I wanted
public static void LoadScene(int levelNum)
{
Application.backgroundLoadingPriority = ThreadPriority.High;
sceneToLoad = levelNum;
SceneManager.LoadScene(loadingSceneIndex);
}
Eror : Assets\Script AR\LoadingScreenManager.cs(35,32): error CS0103: The name 'loadingSceneIndex' does not exist in the current context
and one more problem this one
private void StartOperation(int levelNum)
{
Application.backgroundLoadingPriority = loadThreadPriority;
operation = SceneManager.LoadSceneAsync(levelNum, loadSceneMode);
if (loadSceneMode = LoadSceneMode.Single)
operation.allowSceneActivation = false;
}
Eror : Assets\Script AR\LoadingScreenManager.cs(91,13): error CS0029: Cannot implicitly convert type 'UnityEngine.SceneManagement.LoadSceneMode' to 'bool'

The first error happens because the variable loadingSceneIndex doesn't exist. You should plug the scene you want to load into the LoadScene function like this:
SceneManager.LoadScene(levelNum);
That way the LoadScene function knows which scene to load.
The second error happens because in you're if statement expects a bool, but plug in loadSceneMode = LoadSceneMode.Single. That is defining loadSceneMode. Instead, use loadSceneMode == LoadSceneMode.Single instead because that will return a bool.
Note: Have you looked into using something like Intellisense so your editor can detect these errors for you. If you are using Visual Studio, this link might help.

Related

GetComponent(Type customType) behaving differently than GetComponent<customType>

I have this method 'SetStats' that's part of a class.
public class HeroUnit: MonoBehaviour {
public virtual void SetStats(Stats stats) => Stats = stats;
}
When I instantiate a gameObject by pointing to a prefab, then get the component of a known type 'HeroUnit' attached to it, I can call the SetStats method without any problem.
void SpawnUnit(Vector3 pos) {
var spawnedGameObject = Instantiate(unitPrefab, pos, Quaternion.identity,transform) as GameObject;
var spawned = spawnedGameObject.GetComponent<HeroUnit>();
spawned.SetStats(stats);
}
However, when I don't force a known component type, and that I rather give it dynamically to the GetComponent() method: the instantiation works, but I can't call the SetStats method anymore.
void SpawnUnit(String myUnit, Vector3 pos) {
var spawnedGameObject = Instantiate(unitPrefab, pos, Quaternion.identity,transform) as GameObject;
Type unitType = Type.GetType(myUnit);
var spawned = spawnedGameObject.GetComponent(unitType);
spawned.SetStats(stats);
}
This returns the following compile error :
error CS1061: 'Component' does not contain a definition for 'SetStats' and no accessible extension method 'SetStats' accepting a first argument of type 'Component' could be found
Any idea how to make the compiler understand that 'spawned' is not a 'Component' when the code is ran? I checked 'spawned' type at runtime, and it is a 'HeroUnit', as it should.
Take the var off and see if it'll compile. I don't think you're getting a HeroUnit, I think you're getting a Component.
Try casting the result of GetComponent, like
HeroUnit spawned = (HeroUnit) spawnedGameObject.GetComponent(unitType);
I'll say too that passing types around as strings is a bad idea. You got the string from somewhere, so just use the types instead of strings.
You're probably going to come back and complain about that your unitType isn't necessarily a HeroUnit, to which I would reply, "then how do you know it has a SetStats method?"
You might be better off casting to a base class or interface, but again this string business is not helping you.

Could not use Delegate's Method Property in C#?

When i try to use Delegate's Method Property in C# i get this error.
'myDelegate' does not contain a definition for 'Method' and no
extension method 'Method' accepting a first argument of type
'myDelegate' could be found (are you missing a using directive or an
assembly reference?)
I really don't know the reason why i get this. My program is fairly simple and is based on delegates. Below it's code is given:
public delegate void myDelegate(int x);
public class Program
{
public void Main(string[] args)
{
X x = new X();
myDelegate d = x.InstanceProgress;
Console.WriteLine(d.Method);
}
}
class X
{
public void InstanceProgress(int percent) => Console.WriteLine(percent);
}
I get error on this line:
Console.WriteLine(d.Method);
See this image below, although i get the proper output but i get the error.
I have marked the error with green arrow on the image.
Looking at OP's screenshot. That looks like a Visual Studio error. And since it lets you build the it's not a true error. I don't get that error in VS2015.
I'd clean the solution and restart visual studio. That should clear it.
Old:
You'll want to do something like this:
X x = new X();
myDeligate d = x.InstanceProgress;
d.Invoke(5);
// or as Rahul pointed out you can simply use
d(5);
Invoke() is what actually calls the method. Until then the delegate is just a pointer to the method that you want to call.

An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll. Value cannot be null

I know this has been asked before, but I don't fully understand the answers given as I'm very new to programming.
I've tried adding a null check before the line of code, however, I'm not sure what to do with a "Directory" once it's been checked, if that makes any sense?
public class Config
{
public Config()
{
_random = new Random(DateTime.Now.Millisecond);
_dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
AppDomain.CurrentDomain.RelativeSearchPath,
"TradeAgent",
"Configs");
if (!Directory.Exists(_dir))
{
Directory.CreateDirectory(_dir);
}
}
The error is being thrown on the "_dir = Path.Combine" line.
Can someone break this down for me like I'm 5?
EDIT: I just noticed below this a bit it says:
private string _dir = null;
No idea how to fix this, but maybe that's the problem?
EDIT2: When changing private string _dir = null; to something else like "abc" instead of "null", I no longer get the nullpoint error, although obviously I get a new error saying "the name abc does not exist in the current context".
Again, I don't know how to fix this, but it does seem that _dir is the problem.

Xamarin.Android C# GetItemId() not defined?

For the following code i am getting an error. How do i solve it? I just started android development and dont know anything much.
public override bool OnOptionsItemSelected(IMenuItem item)
{
Type thing = item.GetType();
String id = item.getItemId();}
Error CS1061: Type Android.Views.IMenuItem' does not contain a definition forgetItemId' and no extension method getItemId' of typeAndroid.Views.IMenuItem' could be found. Are you missing an assembly reference? (CS1061) (V002)
For Xamarin Android you have to use item.ItemId instead of item.getItemId()
If you need to cast it to string then:
if(item.ItemId.ToString() == "certainId")
{
//so something here..
}

How do I fix 'compiler error - cannot convert from method group to System.Delegate'?

public MainWindow()
{
CommandManager.AddExecutedHandler(this, ExecuteHandler);
}
void ExecuteHandler(object sender, ExecutedRoutedEventArgs e)
{
}
Error 1 Argument 2: cannot convert from 'method group' to 'System.Delegate'
I guess there are multiple ExecuteHandler with different signatures. Just cast your handler to the version you want to have:
CommandManager.AddExecuteHandler(this, (Action<object,ExecutedRoutedEventArgs>)ExecuteHandler);
I got this error due to a completely different problem.
var engine = new Ingest(GetOperationType, GetSqlConnection);
private static SqlConnection GetSqlConnection(string instanceCode, string defaultDB)
=> new SqlConnection($"Server={InstanceMap[instanceCode]};Database={defaultDB};Trusted_Connection=True;");
private static Type GetOperationType(string operationName)
=> Type.GetType(typeof(BaseOperation).Namespace + "." + operationName + ", ConditioningEngine.EnginePlugins");
Both params to 'new Ingest...' are different types of delegate. The GetOperationType param had no problem while GetSqlConnection got the 'cannot convert from method group' error.
After trying the casting trick mentioned in the other answers the error changed to System.Data.SqlClient not referenced. After fixing the reference problem I could get rid of the cast. That is, the error was false. The casting trick was useful in letting me see what the real error was but the cast itself wasn't necessary. It seems the true error could be almost anything.

Categories