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 /
avatar image
0
Question by crezilinxefizem · Sep 09, 2020 at 04:39 PM · scripting beginnerobject reference

Manipulating one variable in a different script.

I've spent about six hours going through everything on here, stack overflow, and youtube. There's many explanations but I can't get a single one of them to actually work. I am trying to get a variable from another script, manipulate it, and then Debug.Log() all within the second script.

.

GameObject 1: GameMechanics

Script 1: MainGame

Variable: life (Life is also within Global)

.

Game Object 2: Player

Script 2: PlayerInfo

Variable: life

.

I want to get life from MainGame, put it into PlayerInfo, manipulate it across scripts in PlayerInfo, and debug.log it. Every example I find and follow just ends up throwing up about 20 mixed errors and I have no idea why.
Comment
Add comment · Show 1
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 Zaeran · Sep 09, 2020 at 04:44 PM 0
Share

What errors is it throwing? Can you share your code?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Marioooo · Sep 09, 2020 at 06:31 PM

Hi! Hope i can be clear enough

 public Class Script1 : Monobeaviour
 {
     public int variable = 10;
 }

  public Class PlayerInfo: Monobeaviour
  {
      // reference to the gameobject
      public GameObject gameMechanics_GO;
      // local variable, reference to the mainGame Script
      private MainGame mainGameScript;
      private void Start()
      {
          // create the reference
          mainGameScript = gameMechanics_GO.GetComponent<MainGame>();
          Debug.Log(mainGameScript.life);
      }
  }


attach the script to the correct gameObjects and don't forget to drag the manager gameobject to the reference on the player's script... now you can reference the mainGameScript to edit the life variable and you don't need to add a local variable to copy it.. just use the reference

Hope this helps!

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 crezilinxefizem · Sep 20, 2020 at 08:13 PM 0
Share

It has helped enough for me to grasp how to manipulate it accordingly. This is a small test for something bigger that I'm trying to do and it relies heavily on things interacting/being consistent across scripts. Thanks.

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

145 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

Related Questions

creating prefabs in array 2 Answers

Can you trick a Ui Button that it is being clicked with code? 3 Answers

WaitForSeconds - Shooting Script 3 Answers

Helicopter Controls 1 Answer

got an error Object reference not set to an instance of an object 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