Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411508 Posts in 69374 Topics- by 58430 Members - Latest Member: Jesse Webb

April 26, 2024, 09:50:32 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Why aren't my values getting assigned?
Pages: [1]
Print
Author Topic: Why aren't my values getting assigned?  (Read 561 times)
DCoward
Level 0
**



View Profile WWW
« on: July 18, 2016, 01:42:00 PM »

Hey all,

After some frustrating realization, I found that I might not be screwing up serialization after all (ha).

But I have found that for some reason, my values are not getting assigned?

Let me demonstrate.

Code:
using UnityEngine;
using System.Collections;
using System; //Last 3 for Serialization
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

[System.Serializable]
public class GuardA : MonoBehaviour {

public StandardPlayerParadigm profile;
private Mercenary job;
private GuardPrototype type;

// Use this for initialization
public GuardA() { //I have also tried to use void Awake() and void Start() to no avail

profile = new StandardPlayerParadigm ();
job = new Mercenary ();
type = new GuardPrototype();

//decl the stats

profile.Name = "Guard A";
profile.stdType = type.stdType; //Which child of StandardType?
profile.Profession = job.Job; //Class
profile.ProfessionDescription = job.JobDescription; //ClassDescription
profile.Level = 1;
profile.Health = type.Health;
profile.Mana = type.Mana;
profile.TP = type.TP;
profile.Attack = type.Attack;
profile.Defense = type.Defense;
profile.MagicAttack = type.MagicAttack;
profile.MagicDefense = type.MagicDefense;
profile.Critical = type.Critical;
profile.Dexterity = type.Dexterity;
profile.HitRate = type.HitRate;
}

// Update is called once per frame
void Update () {

}
}


This code borrows from three other scripts, so let me break down their purpose before showing the code.

  • StandardPlayerParadigm controls what a character SHOULD have at all times. I made StandardPlayerParadigm profile in GuardA so that I could load the values TO the comprehensive list of variables.
  • Mercenary is a job that will eventually control weapons and armor choices but for now just has the name of the job (Mercenary) and the description of it so I can see it is working.
  • Finally, the GuardPrototype has a list of actual values for the player stats, and as such, I want to assign those as well to the variables available through profile.

Now, I don't know that I could make this thread look good by including ALL of the code bits on the page, so here is my gist that I used previously in a different thread. https://gist.github.com/alucard55/61df6f96fef4d9111ec98315a4f8a785

None of the code is different in essence, and contains the full line of inheritance that the scripts borrow from etc and so forth until I want it to be used in GuardA.cs .

Now here's the TL;DR (sort of) for this.

When I have the part in GuardA where I say profile.Name = "Guard A"; etc and so forth to the end, none of these get assigned. However, I know the values are available, because if I make private Mercenary job/private GuardPrototype type into public variables, then I can see in Unity's inspector that the values are definitely there, I'm just not able to GET these values and put them in the variables I want, I guess.

So now TL;DR: Why is this not getting assigned? What do I need to do in order to do it, and is there anything I should know going forward about this sort of thing (as in, is it easy to mess up like I did, or did I just do a dumb)?
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
oahda
Level 10
*****



View Profile
« Reply #1 on: July 18, 2016, 01:55:11 PM »

That is seriously weird. I can't see anything in your code that should cause trouble setting the value, so maybe you're getting them incorrectly somehow... Where and how are you getting them? Did you see the right values in the inspector when you made them public?

Quick note: MonoBehaviour is marked serialisable already, so you don't need to do that explicitly yourself.
« Last Edit: July 18, 2016, 11:19:57 PM by Prinsessa » Logged

DCoward
Level 0
**



View Profile WWW
« Reply #2 on: July 18, 2016, 02:21:03 PM »

That is seriously weird. I can't see anything in your code that should cause trouble setting the value, so maybe you're getting them incorrectly somehow... Where and how are you getting them? Did you see the right values in the inspector when you made them public?

Quick note: MonoBehavior is marked serialisable already, so you don't need to do that explicitly yourself.

I mean I might be, but I'm getting the values (which are all public...) from other scripts (included in the gist link).

For instance, GuardPrototype has the stats, and that's what I called and tried to set... And yes, the values DO show correctly when I try to change from private to public.

I mean, the only other point of note is that the things I am calling are all inheriting something else, but.... that doesn't seem like it should matter?
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
oahda
Level 10
*****



View Profile
« Reply #3 on: July 18, 2016, 11:33:05 PM »

Is it the OnGUI() stuff setting the labels that isn't working?
Logged

bateleur
Level 10
*****



View Profile
« Reply #4 on: July 20, 2016, 02:14:31 AM »

So OK, first thing to note is that you shouldn't write Constructors for your Unity components. They aren't called when you think they are and just generally don't do what you want. If you think you want to use a Constructor (for example because you're having trouble with the ordering of Awake() calls) you're still probably wrong. I'd strongly recommend getting out of the habit.

With that said, you do make clear you've tried using Awake, so the Constructor may not be the source of your problems.

What I'd recommend is adding Debug.Log lines at the end of your function which print the values of the fields you've tried to assign. I'd bet good money that either these lines are never reached or they print correctly assigned values. Whatever your problem is, it's not here.
Logged

oahda
Level 10
*****



View Profile
« Reply #5 on: July 20, 2016, 02:32:47 AM »

Oooh, right, didn't think about the fact that they're MonoBehaviours and about when to use them or how those get constructed. I've been in C++ mode the last few days. Yeah, I never use constructors on MonoBehaviours in Unity either, so that might be good to think about.

And yeah, I was going to suggest something similar depending on what OP replied regarding OnGUI()...
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic