Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Cryonize · Sep 07, 2016 at 12:45 PM · reference

Instantiated Object has proper integer value after functions were called but returns 0 when referenced.

I honestly don't know if the topic of this is actually correct. Anyway, to make it clearer, I have BulimawClass class as the base script of the BlackSagoScript script that's attached to a Prefab.

This is a snippet of the BulimawClass:

 ...
 
 //bulimaw level
     [SerializeField]
     private int level = 0;
     public void setLevel(int x) { level = x; }
     public int getLevel() { return level; }
 
     //bulimaw minimum level for bulimaws
     [SerializeField][HideInInspector]
     private int minLevel;
     public void setMinLevel(int x) { minLevel = x; }
     public int getMinLevel() { return minLevel; }
 
     //bulimaw maximum level for bulimaws
     [SerializeField][HideInInspector]
     private int maxLevel;
     public void setMaxLevel(int x) { maxLevel = x; }
     public int getMaxLevel() { return maxLevel; }
 
     //function to randomize level of bulimaws
     public void randomizeLevel(){
         setLevel (UnityEngine.Random.Range (getMinLevel (), getMaxLevel ()));
     }
 
 ...

And here's a snippet of the BlackSagoScript attached to the Prefab that I'm going to instantiate:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class BlackSagoScript : MonoBehaviour {
 
 
     public BulimawClass blackSago;
     public BulimawList bulimawList;
     public SkillsList skillsList;
     private SkillClass addSkill = new SkillClass ();
 
     void Start () {
         setWildData ();
         blackSago.randomizeLevel ();
         blackSago.calculateInitialAccumulatedExp ();
         blackSago.setRandomGender ();
         blackSago.calculatePhysique ();
         blackSago.randomizeBase ();
         blackSago.initialStatDistribution ();
         fillSkillPool ();
         randomizeSkills ();
     }
 
 ...

So, I instantiate it on this script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class DataLoader : MonoBehaviour {
 
     public GameObject BlackSago;
 
     void Start () {
         GameObject bSago = (GameObject)Instantiate (BlackSago);
         bSago.GetComponent<BlackSagoScript> ().blackSago.setMinLevel (5);
         bSago.GetComponent<BlackSagoScript> ().blackSago.setMaxLevel (10);
 
         print (bSago.GetComponent<BlackSagoScript> ().blackSago.getLevel ());
     }
 
 }

When I look at the instantiated object, I see this in the inspector: alt text

But when I try to print the level of the instantiated object from the DataLoader script it returns 0. Everything else returns zero, actually. The only thing that's actually correct is the minLevel and maxLevel. I'm desperate now, lol.

capture.png (8.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ScaniX · Sep 07, 2016 at 01:05 PM

You are setting the level only in the Start() method which will not have been executed at the time you are printing the level.

I think if you move your initialization to the Awake() method, your values will be initialized during the Instantiate() call.

Also see here: http://forum.unity3d.com/threads/instantiate-prefabs-and-start-awake-functions.197811/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pass reference to variable in IEnumerator method in C#? 2 Answers

Compute Shader Pass RWStructuredBuffer by Reference 1 Answer

can I use 'ref' here to get current position of selected Game Object 1 Answer

component not detecting parent class 0 Answers

Application Error after baking lightmaps 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges