- Home /
I am not explaining the problem properly.... due to my lake of proper terminology
I am very new to programing > I have a script attached to a cloned game-object and need help referancing ..... Any help would be wonderful ^_^
I'll try to explain the problem I'm having, a little better.............................. I have script_A attached to a cloned game-object....... the script_A works just fine......... when I try to reference script_A from script_B..... I get a null reference error.............. I don't get errors referencing other scripts..... just this one.............. My guess is because it is on a clone...... so the script is multiplied (cloned) on each copy of the game-object (clones).............. and that is exactly what I want.... each clone runs script_A and they all collect their variables separately for each ..................... but then how do I reference script_A...... when it is really multiple-script_A's............... ???...................... I hope someone can understand my question.... sorry I can't explain it any better because I am very new to programming.......... any help would be great.... thanks
Did you write this yourself? Your question seems to be "how do I access something in another script", ya?
yes, I wrote this script myself......I just don't know how to access the value from a dictionary, that is on a script running on a prefab cloned game-object ... need to get the dictionary tag value that is on this script > from another script without getting a null reference error..... I have many other references between scripts that are all running just fine..... I just can't get a reference to this script for some reason ???... .......................................................I am very new to program$$anonymous$$g..... and trying my best to learn.... the code is a mess I know..... but I have been trying several ways to get at the value I need from this script.......... this script (not the problem) does work as I need it to.... updates very well in the Inspector & my debug to console works as intended also...... I just can't use the dictionary tag value in another script..... ANY help would be great...... please and thanks
P>S> my dictionary is already on a game object prefab, that has 7 clones.... the script is running great and stoes the movement value for each clone indepentently.... the script shows the value in the inspector (for each clone) just fine....... my problem (BIG one) comes when i try to use the value to do something with it in another script......... ie: something like: ArrayPlaneColorSquares_P1 [temp_Unity_Question.IntValueSumOf_UpdatedCurrentDiceTotalPlus_ThisCloneP1] .gameObject.SetActive(true); .....................
[temp_Unity_Question.F_ReturnValue_DiceTotalPlus_ThisCloneP1()] .gameObject.SetActive(true);............................... no matter witch way I try to use the value in another script I always keep getting Null Refrance Exeption Error ..................................................................... my dictionary stores > game-object (tag), int (value) >>> working fine in the script above ....... I need the int(value) from this dictionary >>> to use the int as a reference to a spot in an array.... array[ this int ]......... but when I try to get the value from within another script.... I can't....... I always get a null reference.... ............... $$anonymous$$aybe there is no way to do it................. $$anonymous$$aybe I just have to do everything I need to do inside the script containing the dictionary ???the value >>> "IntValueSumOf_UpdatedCurrentDiceTotalPlus_ThisCloneP1" is the one that I need .................................... I also tried >>> player1_Highlights.ArrayPlaneColorSquares_P1
Answer by Refzlund · Apr 24, 2019 at 12:03 AM
Debugging code requires readable code
I'd start by saying that this is really difficult to read. Not because it's hard code, but because the naming conventions are incomprehendable.
I litterally have to use the Highlight All feature in my webbrowser to find the same variables. Not very smart. Good code is readable code.
I'd love to dig into your code, but the naming convention really would make it more of a hazzle than in needs to be.
I honestly have no idea what this code does. Here's a guess of what you're trying to tell me through your naming conventions (which might be off).
[Header("P1 / Number Of Total Tile Moved / To date")]
public int startTile; // Dic_INT_Value
public int currentTile = 0; // TEMP_CurrentTileNumberTotal_P1
public int nextTile; // IntValueSumOf_UpdatedCurrentDiceTotalPlus_ThisCloneP1
public List<int> moves = new List<int>(); // TEMP_PlayerStone1_MoveNumCount_List
public Dictionary<PlayerStone, int> playerStoneTile = new Dictionary<PlayerStone, int>(); // TEMP_PlayerStone1_MoveNumCount_Dictionary
public PlayerStone playerStone; // THIS_PlayerStone
public int sumOf_DiceTotalPlus_ThisCloneP1; // ???
void Start()
{
// Add current tiles to moves
moves.Add(currentTile);
playerStoneTile.Add(playerStone, currentTile);
Debug.Log("PlayerStone Moves: " + playerStoneTile.Count);
// Get PlayerStone tile
startTile = currentTile;
}
void Update()
{
if (Static_Class_Variables.CurrentPlayerId == 0)
{
nextTile = ((startTile) + Static_Class_Variables.Global_DiceTotal_Copy);
}
}
Answer by xephosmmb122 · Apr 24, 2019 at 12:46 AM
Hi what I believe you are trying to say is that you are having trouble referencing another variable from another script. Your code is very hard to read and understand, I would suggest learning how to use camel casing and use general naming conventions IE: Shorten TEMP_PlayerStone1_MoveNumCount_List to just playerStone1_Move
to reference another a variable from another script you must get the script in your Start method
testScript testScriptReference = testScriptReference.GetComponent();
Then from here you can reference by using
testScriptReference.testVariable
Answer by Laz0007 · Apr 24, 2019 at 02:31 AM
Thanks for trying to help...... I know this does not conform to standard C# conventions.... but I have been coding for about 3 months...... I'm learning as I go........ My strength is in graphics..... It's not that I don't want to learn all the proper coding conventions................... but that is secondary to getting things done right now................... I really shouldn't have posted any of the code......... my question has more to do with referencing the script ........ this script is attached to a clone........... I can't seem to reference it, in the same way, I reference other scripts................ this script is not the problem..... it's referencing this script from another script that is the problem............ Thanks anyways.......... I'm going to cut the code out of my question
Follow this Question
Related Questions
Changing a dictionary value from another script 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Reference to value type 1 Answer
Combine Children Dictionary in place of Hashtable? 2 Answers