Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 dkovacs150 · Mar 02 at 03:42 PM · scripting problemscripting beginnerfunctions

How do I assign values to variables in the Update function only ONCE?

Hello there!

I'm making a game that has a weapon customization system. This way a player can alter their weapon's statistics to their liking with different attachments. Every attachment is a game object (prefab) that has their own statistics, that are connected to the weapon's script using interfaces. Here is what I've got so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 
 
 public class BRN180 : MonoBehaviour
 {
     
      public string Type = "Brownells BRN-180 16 inch";
      public float Weight = 2.0f;
      public float Cal = 5.56f;
      public float baseDamage = 20.0f;
      public float MOE = 1.5f;
      public float reloadTime = 3.2f;
 
 
 
     public GameObject opticUsed; 
     public GameObject opticObject;
      Optic opticStats;
 
     public GameObject magazineUsed; 
     public GameObject magazineObject;
      Magazine magazineStats;
 
      public GameObject stockUsed; 
     public GameObject stockObject; 
      Stock stockStats;
 
 
 
     private SpriteRenderer partRenderer; 
     private SpriteRenderer objectRenderer;
     
     
     void Start()
     {
     }
 void OnGUI(){
      GUI.Label (new Rect (200, 100, 300, 60), "Weapon's name: "+Type);
   GUI.Label (new Rect (200, 150, 300, 60), "Weapon's weight: "+(Weight+opticStats.Weight+magazineStats.Weight+stockStats.Weight)+" kilogramms");
   GUI.Label (new Rect (200, 200, 300, 60), "Weapon's damage: "+(baseDamage*Cal));
   GUI.Label (new Rect (200, 250, 300, 60), "Weapon's accuracy: "+(MOE-opticStats.Accuracy*stockStats.Stability)+" MOE");
   GUI.Label (new Rect (200, 300, 300, 60), "Weapon's capacity: "+(magazineStats.Capacity)+" rounds");
 
 
 }
     // Update is called once per frame
     void Update()
     {
             opticStats = opticUsed.GetComponent<Optic>();             
             partRenderer = opticUsed.GetComponent<SpriteRenderer>(); 
             objectRenderer = opticObject.GetComponent<SpriteRenderer>(); 
             objectRenderer.sprite = partRenderer.sprite; 
 
 
 
             magazineStats = magazineUsed.GetComponent<Magazine>(); 
             partRenderer = magazineUsed.GetComponent<SpriteRenderer>(); 
             objectRenderer = magazineObject.GetComponent<SpriteRenderer>(); 
             objectRenderer.sprite = partRenderer.sprite; 
 
 
 
             stockStats = stockUsed.GetComponent<Stock>(); 
             partRenderer = stockUsed.GetComponent<SpriteRenderer>(); 
             objectRenderer = stockObject.GetComponent<SpriteRenderer>(); 
             objectRenderer.sprite = partRenderer.sprite; 
 
     
     }
 }

This displays the current statistics of the weapon, but the statistics don't actually change... If I try calculating them as I do when I write them on a Label, it keeps getting recalculated each frame.

So, how do I go about changing stats, only when an attachment changes?

Thank you in advance!

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
Best Answer

Answer by Paolofix21 · Mar 11 at 05:15 AM

You actually don't.

When getting components to store them inside variables the best approach is to do it inside the Start() or even better the Awake().

The Awake() gets called when the object is first loaded or instantiated. The Start gets called after the Awake once, when the object is activated the first time, but before the Update.


Anyway, if you ever for some particular reason need to avoid setting a variable that's already been set, you can use various methoss:

 if(!ReferenceEquals(null, variableName))
     variableName =...;

or

 variableName ??=...;

or (but this is not good for performance)

  if(variableName != null) 
     variableName =...;

I hope this can help you :)

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 dkovacs150 · Mar 29 at 04:13 PM 0
Share

Thank you! I was kinda late to reply, but thank you for your response.

I eventually found my own solution around the problem, but I also tried this for good measures, and it'd work lol

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

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

Need help with pause menu and cursor lock script conflict 1 Answer

GameObject.Find : why not working? 3 Answers

Fading in Smoothly between Sprites 0 Answers

How can I replace objects in the initial position? 0 Answers

Why can I only move upwards? 1 Answer


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