Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm working on a talents/skills tree system for my game made with Unity and I've got a class for my 45 talents buttons that looks like this:
public class SaveTalentClass {
public int talId;
public int talCurRank;
public int talMaxRank;
public SaveTalentClass(int id, int cRank, int mRank)
{
talId = id;
talCurRank = cRank;
talMaxRank = mRank;
}
}
I created 10 "Talent Lists" so the player can save different talents and I stored these 10 Lists in another List for easier access. So I've created a 2D list like that:
public List<List<SaveTalentClass>> containerList = new List<List<SaveTalentClass>>();
And added the 10 "talent Lists" into it but now I'm stuck trying to access/write in this 2D List.
I've tried a test like:
containerList[0][0].Add (new SaveTalentClass(0,1,2));
but got an error: SaveTalentClass' does not contain a definition forAdd' and no extension method Add' of typeSaveTalentClass' could be found (are you missing a using directive or an assembly reference?)
I'm pretty sure there's an easy fix for that but I couldn't figure out how to do it !
Thanks for helping me :)
Giving two indexes, you're already inside the second list. Either, if you want to add a new element to one of the lists inside the outer list, use
containerList[0].Add (new SaveTalentClass(0,1,2));
Or if you want to modify an existing one:
containerList[0][0] = new SaveTalentClass(0,1,2);
Since you have the containerList, you can add an object of the type on it, in that case is a List of Lists. So, add a new list of SaveTalentClass and after you can access it by 0 index to add an object of SaveTalentClass object, for sample:
// add the first list in the containerList
containerList.Add(new List<SaveTalentClass>());
// add an item in the first list of containerList
containerList[0].Add(new SaveTalentClass(0,1,2));
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
void Convert(string word)
{
var dictionary = (JObject)JsonConvert.DeserializeObject(dictionaryJson.text);
if(dictionary[word] != null)
{
dictionary[word].ToObject<VocabularyModel>();
// do something;
}
else
{
// do something;
}
}
I am using (JObject)JsonConvert.DeserializeObject to convert json to var dictionary. Everything works fine. However I only have 1 json file and every time a use the function I have to:
var dictionary = (JObject)JsonConvert.DeserializeObject(dictionaryJson.text);
Is there any way to call that line only one and the var dictionary is saved as global variable or something similar that help me call the above command only one time only.
And one other question: does the dictionary[word] works like a loop in an array? Does it go through the dictionary one by one and search for the key "[word]" ?
You can move the declaration of into a class-level scope.
class WhatEverYourClassNameIs
{
private var dictionary;
// Deserializing in constructor, but you can do it elsewhere
WhatEverYourClassNameIs()
{
dictionary = (JObject)JsonConvert.DeserializeObject(dictionaryJson.text);
}
void Convert(string word)
{
// This function and all other functions within your class will have access to dictionary
}
}
Regarding your second question, I don't have a concrete answer since the documentation doesn't say so. A normal .NET dictionary has O(1) indexing complexity, meaning that it doesn't take longer the more elements you have in the dictionary. If you want to be certain you'd have to look up the source code and see how indexing is accomplished in the JObject class
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to clear a static list from my "playerStats" script which I use to access static variables globally. Adding elements to the list works just fine (with playerStats.myList.Add(levelNumber)), but when I try to use myList.Clear I get this error:
"Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement".
Here is the relevant code: First the list definition inside the playerStats class:
{
public static List<int> myList = new List<int>();
}
And this is then on another script referencing the playerStats:
public void goBack()
{
playerStats.myList.Clear;
SceneManager.LoadScene(0);
}
Why can't I clear the list like this?
This error essentially says that you are taking a reference to a method, but you are not assigning it to anything that can reference it. The compiler doesn't know you are actually trying to call this method.
You need to add parentheses, as in
playerStats.myList.Clear();
to call the method.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have been using Visual Studio Code for my C# code; however, it does not seem to realise that I have got a curly bracket '}' at the end of the constructor for my 'opponent' class. Please help me find a way to make it realise that I have done it correctly as far as I can tell!
Here is the class:
public class opponent
{
public int hp = 20;
public int PDmg = 3;
public int KDmg = 3;
public void Opponent(int lv = 1)
{ # red line error marker here
public int level = lv;
}
}
And here is the error message:
} expected [miscellaneousFiles.csproj]
Please help!
I have tried changing the name and capitalisation of the method name but it has not worked so the case-sensitivity of C# is not the problem. I have also tried removing the word 'void' and it did not work either.
double click on the error and it will take you to the place you have missed the brace maybe its not this code where you have got the error
From this line
public int level = lv;
remove public and its gonna work
C# is case-sensitive language, so Opponent and opponent are not the same ;)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I honestly could not think of a title...anyways
I am trying to create a type
public enum AssemblyType{
Name = "NameOfFile.dll",
}
In then in the unity inspector I would have a list of this type
public List<AssemblyType> requiredAssemblies = new List<AssemblyType> ();
And my expectation is that I will see a list where I can set a list of dropdown boxes to the Name of the AssemblyType
For example this
Inside of this
In case you dont get the image
Day Colours would be the list requiredAssemblies and size would be like Culling Mask on the first image
Sorry forgot what went wrong with the above method
This method returns this error in unity
Assets/Scripts/WebSharp.cs(21,16): error CS0029: Cannot implicitly convert typestring' to int'
sorry my problem was clearly stated
from the error I would(as anyone i hope) realize that an enum is an int value
My question what should I be replacing the enum with since a string cannot exist outside of a class
This
public enum AssemblyType{
Name = "NameOfFile.dll",
}
...doesn't compile. So I'm not sure what the question is.
If you're just trying to define a list of DLL names, you just need to replace this
public List<AssemblyType> requiredAssemblies = new List<AssemblyType> ();
with this
public List<string> requiredAssemblies = new List<string>
{
"NameOfFile.dll",
"NameOfOtherFile.dll",
"Et cetera"
};
If you need to expose a simpler name to the user, but let the application remember the original name, you can use a dictionary:
public Dictionary<string,string> requiredAssemblies = new Dictionary<string,string>
{
{"NameOfFile1.dll",#"c:\SomeLongPath\NameOfFile2.dll"},
{"NameOfFile2.dll",#"c:\SomeLongPath\NameOfFile2.dll"},
{"NameOfFile3.dll",#"c:\SomeLongPath\NameOfFile3.dll"}
};
..and then populate your controls using the list returned by requiredAssemblies.Keys. You can then use the user's selection to look up the full path in the dictionary when you need it.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm getting the error:
Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)
on multiple lines(20+), I know what this error means so I changed all of my int declarations to float but this didn't change where the error was appearing so I had a look at the lines and noticed it was to do with a list declaration I had in a different class.
the error appears on lines like this one:
graph = new Node[widthX, heightY];
It says the error is on widthX and heightY but both are declared as floats, graph however is declared as:
Node[,] graph;
Which is a different class and inside it is a list which I think, is where the root of this error is actually coming from. The code of that class looks like this:
public List<Node> edges;
public Node(){
edges = new List<Node> ();
}
After reading up on C# I saw a lot of converting from string to int or int to string or double to int etc, but nothing on floats.
Or I'm reading this error incorrectly in which case I have no idea where its coming from.
You can't use widthX and heightY in your array declarations, if they are not integers. If that makes sense, you have to cast them to int.
var graph = new Node[(int)widthX, (int)heightY];