Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 nick9090 · May 30, 2015 at 05:21 PM · basics

How to connect Scripts and Objects correctly

Hi, this is a very basic Question, because i offen run in this Kind of problem. On one script I can read variables from other scripts and the next time this does not work. So I thought I make an class to store important things where I can access easily from every script, but don't know how to realize it in unity.

The problem I currently have is the game over. I need this information for the player, enemys, score and so on. But I can't access them everywhere the same way and don't know how to get this to work.

Most the time I get the error object reference not set to an instance of an object. But sometimes it just works and this makes it very difficult to find out why it does not work correctly.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Crystalline · May 30, 2015 at 06:25 PM

Create a script containing static variables ( ex : public static int max_inventory )

Also, in the same script static functions ( ex : public static void addXpPoints () )

Its all about using static variables. You can call anything just by referencing the class name of the script.

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 nick9090 · May 30, 2015 at 08:43 PM 0
Share

Thx, but I found another way to get what I wanted. I use a Script I called Connector.cs bind it to the $$anonymous$$ainCamera and access it then from there. I also use getter and setter method(getHealth(),setHealth()) in this connector script. $$anonymous$$y own Answer is awaiting approval, but should be avaiable in a short time.

avatar image
0

Answer by nick9090 · May 31, 2015 at 08:21 AM

I managed it myself by binding a script to the MainCamera. I called it Connector

This is how the Connector.cs looks like using UnityEngine; using System.Collections;

 /*
  *      Script used for sharing Variables between several Scripts
  * 
  */
 
 
 public class Connector : MonoBehaviour {
 
     public int playerHealth;
     public bool gameOver = false;
     public bool isPause = false;
     public bool resetPlayerPrefs = false;
 
     // Use this for initialization
     void Start () {
         if(resetPlayerPrefs){
             Debug.Log("Delete PlayerPrefs is Active!");
             PlayerPrefs.DeleteAll();
         }
     }
     
     // Update is called once per frame
     void Update () {}
 
     public void setPlayerHealth(int health)
     {
         playerHealth = health;
     }
 
     public int getPlayerHealth()
     {
         return playerHealth;
     }
 
     public void setGameover(bool gameover)
     {
         gameOver = gameover;
     }
 
     public bool getGameover()
     {
         return gameOver;
     }
 
     public void setPause(bool pause)
     {
         isPause = pause;
     }
     
     public bool getPause()
     {
         return isPause;
     }
 
 }

And thats how to access this anywhere in other Scripts:

 GameObject ob; //Connector, Top of the Class

In start(){} I add:

ob = GameObject.FindGameObjectWithTag("MainCamera");

And thats how I access Variables:

ob.GetComponent().getPlayerHealth();

I think this Solution is a very clean one. It is possible to access the Variables directly, but i like to use Getter- and Setter-functions.

Now I have cleaned up my Code and don't have the problems with accessing everything from everywhere.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Basics of Perlin Noise? 1 Answer

Mobile "play by email" network basics, send game move via server 1 Answer

Fundamental question about Update() 1 Answer

`ActivateTrigger.target' is a `field' but a `type' was expected 1 Answer

How is the following code from 'Space Shooter' Tutorial Working? 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