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 Ochreous · Mar 25, 2014 at 10:52 PM · c#gameobjectvalues

C# Gameobject's Script's ValuesEquals Other Gameobject's script's Values

I have two gameobjects with have the exact same script attached to both of them. I would like one of the gameobjects's script's values equal the other gameobjects's script's values. But I keep getting the error the left hand side must contain a proper indexer. What am I doing wrong?

 public class SomeScript1 : MonoBehaviour {
     
     public gameobject someGameObject1;
     public gameobject someGameObject2;
     
     void Start(){
     //Both Gameobjects contain the script SomeScript2
     someGameObject1.GetComponent<SomeScript2>() = someGameObject2.GetComponent<SomeScript2>();
     }
 }
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 getyour411 · Mar 25, 2014 at 11:01 PM

Instead of public gameObject why not

 public SomeScript2 someScript2;

(drag/drop gameobjects containing that Components in Inspector)

and then

 myVar = someScript2.myVar;
Comment
Add comment · Show 7 · 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 Ochreous · Mar 26, 2014 at 12:07 AM 0
Share

I tried that and I couldn't find the gameobject in someScript2 . When I saved it I got an error saying that someScript2 doesn't contain a definition of someGameObject2. I also tried someScript2.gameobject and someScript2.transform but those variables are read only.

 public class SomeScript1 : $$anonymous$$onoBehaviour {
      
     public gameobject someGameObject1;
     public SomeScript2 someScript2;
      
     void Start(){
     someGameObject1 = someScript2.someGameObject2;
     }
 }
avatar image getyour411 · Mar 26, 2014 at 12:10 AM 0
Share

Your problem statement said you want to assign script variables why do we keep going round with gameobjects?

thisScriptVar = someScript2.thatScriptVar;

avatar image Ochreous · Mar 26, 2014 at 12:48 AM 0
Share

Because I want to change the scripts specifically attached to those gameobjects. Lets say in someScript2 there is an integer called someInt.

 public class SomeScript2 : $$anonymous$$onoBehaviour {
  
 public int someInt;
 }

I then assign someInt a value for each gameobject. For someGameobject1 someInt equals 5 and someGameobject2 equals 10. Ins$$anonymous$$d of typing someGameObject1.GetComponent().someInt = someGameObject2.GetComponent().someInt I've been trying to see if I can do the exact same thing except with the classes.

         public class SomeScript1 : $$anonymous$$onoBehaviour {
          
         public gameobject someGameObject1;
         public gameobject someGameObject2;
          
         void Start(){
         someGameObject1.GetComponent<SomeScript2>().someInt = 5;
         someGameObject2.GetComponent<SomeScript2>().someInt = 10;
         //Both Gameobjects contain the script SomeScript2
         someGameObject1.GetComponent<SomeScript2>() = someGameObject2.GetComponent<SomeScript2>();
         }
     }


avatar image getyour411 · Mar 26, 2014 at 01:04 AM 1
Share

You want to look at Send$$anonymous$$essage to see if that's something you could use. I still don't get why someint = script2.someint is a problem, but I don't want to beat a dead horse.

avatar image Ochreous · Mar 26, 2014 at 01:40 AM 0
Share

Sorry I made a mistake with the code. It's fixed now.

  public class SomeScript1 : $$anonymous$$onoBehaviour {
      
     public gameobject someGameObject1;
     public gameobject someGameObject2;
      
     void Start(){
       someGameObject1.GetComponent<SomeScript2>().someint = 5;
       someGameObject2.GetComponent<SomeScript2>().someint = 10;
       //Both Gameobjects contain the script SomeScript2
       someGameObject1.GetComponent<SomeScript2>() =   someGameObject2.GetComponent<SomeScript2>();
     }
  }
Show more comments

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

19 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

Related Questions

C# Check If Gameobject is within Collider 1 Answer

C# Preserving GameObjects' Previous Meshes 1 Answer

C# Plane Detecting a Gameobject 1 Answer

C# Reverting GameObject to Original 1 Answer

C# Rotate More than Two GameObjects 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