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 Dejan90 · Oct 24, 2016 at 06:51 PM · c#bug-perhapsintsingleton

Setting two int equal each other, is causing a different behaviour, than two int arrays equalling each other

Hello, I'm not sure if this is a Bug, or if there are other reasons for this problem but assinging an int[] (Script2) to equal another int[] (Script 1) is causing both variables to be connected, so by changing the value of second int[](Script 2) is automatically updating the first int[] (Script 1) . By doing exactly the same with two normal int the behaviour is a completly different one. By Updating the int of Script 2, the int of Script 1 is staying unchanged.

So how can the behaviour of the same action end up being so different?

Script 1

 using UnityEngine;
 using System.Collections;
 
 public class Script1 : MonoBehaviour {
     public static Script1 Script1Singleton;
 
     public int TestInteger =0;
     public int[] TestIntegerArray ={0};
 
     // Use this for initialization
     void Awake () {
         Script1Singleton =this;
     }
 }

Script 2

 using UnityEngine;
 using System.Collections;
 
 public class Script2 : MonoBehaviour {
     public int TestInteger =0;
     public int[] TestIntegerArray ={0};
 
     // Use this for initialization
     void Start () {
         TestInteger = Script1.Script1Singleton.TestInteger;
         TestIntegerArray = Script1.Script1Singleton.TestIntegerArray;
     }
     
     // Update is called once per frame
     void Update () {
         TestInteger++;
         TestIntegerArray[0]++;
         print("Script1 Int: "+Script1.Script1Singleton.TestInteger+ " Script2 Int:"+TestInteger);
         print("Script1 Int[]: "+Script1.Script1Singleton.TestIntegerArray[0]+ " Script2 Int[]:"+TestIntegerArray[0]);
     }
 }

Console Log

alt text

unityproblem.jpg (146.1 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
1
Best Answer

Answer by hexagonius · Oct 24, 2016 at 07:52 PM

welcome to the world of programming, introducing value type vs reference type. While an integer is a value type, directly inheriting it's value,a reference type inherits the address to it's value. int a = 1.
int b = a // b and are 1.
b++ // a stays 1, b is 2.

int[] a = {1}
int[] b = a // b now "points" to a's array.
b[0]++ // a[0] and b[0] both read 1, because they point to the same value.

this means, assigning one array variable to the other copies the address to the actual array. from now on indexing into it works the same from both variables

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 Dejan90 · Oct 24, 2016 at 08:25 PM 0
Share

Oh that's why, thanks for the quick answer. You helped me a lot!

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

245 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

Related Questions

Singleton ScriptableObject unable to Find itself, returns null 2 Answers

Access singleton in OnEnable when it's initialized in Awake 3 Answers

Convert int to binary 1 Answer

Values in array won't change and compile [Bug?] 1 Answer

Word guessing game 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