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
1
Question by Pigzooka · May 15, 2019 at 09:02 AM · variablesstaticnoobvariable-definition

Question About Defining and Calling Static Variables

Hello,

I am a very new Unity user and I am trying to make a game that will include character customization. I would like to ask players to define their character's height. I am not at all sure how to do this. My strategy was going to be to have one script that is basically a list of all variables that will define and track them (this script will be known as VariableList), and then another script that will allow me to change the height value when the player clicks a button to choose their height (this one is known as VariableChanger). The Height variable is defined like so, in VariableList:

 using UnityEngine;
 using System.Collections;
 
 public class VariableList : MonoBehaviour
 {
     static public string Height;
     // Use this for initialization
     void Start()
     {
         Debug.Log("My height is " + Height);
     }
 
 }
 

It also features a little debug function that I have been trying to use. Then I would like to call this variable in another script, VariableChanger, which I will plug in to my buttons. This is where things start to go wrong. I have absolutely no idea how to write such a script. Here is my attempt so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class VariableChanger : MonoBehaviour
 {
     VariableList.Height = VariableList.Height
     public string HeightUpdater(string VariableList.Height)
     {
         VariableList.Height = "56";
         Debug.Log("My height is now " + VariableList.Height);
         return VariableList.Height
     }
 
 }
 

VariableList.Height = VariableList.Height was my effort to resolve an error where the script didn't seem to be able to fetch the static variable Height from the other script, but it didn't help. I sort of doubt that this is the right approach in general, but this code in particular is full of errors. Even when I tweak it to try and remove the errors and allow the game to run, no option appears when I plug it into the On Click() property of the button to update VariableList.Height or have it do that on click or anything like that. I am very confused, and help would be appreciated!

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 tormentoarmagedoom · May 15, 2019 at 11:02 AM

First,

Why this line in VariableChanger ? what you think it does?

 VariableList.Height = VariableList.Height

Sincerly, you are trying to build a car without learning before to build a bike...

Before commencing create functional scripts, learn:

private variables public variables

learn how must be called, readed or writed

then learn what is a static variable and how they work

And after that, try to do some simple and basic scripts using what you learnt.

Reallym, spend one week only learning, reading, watching tutorials about the same again and again, ultin you are sure you know how they work.

If you try to build a house but doesn know about materials, the house will fall for sure.

Good luck!

Comment
Add comment · Show 1 · 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
avatar image Pigzooka · May 15, 2019 at 03:32 PM 0
Share

I am trying to learn all of this by doing. I've found that with a lot of things if I get bogged down in theoretical stuff I never actually learn how to use the skill I am supposed to be developing. What I would like is specific advice about how one would go about updating a static variable that's defined in one script using a function in another script.

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

178 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 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 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

Change of static variable 1 Answer

Scene stores variables after scene reload 0 Answers

Game Objects not using same script independently. 1 Answer

Static Class Strings not truly null? 1 Answer

Setting public static variables in a static class 2 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