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
-3
Question by F-N · Apr 01, 2014 at 06:46 PM · changetagsdo not

Changing the type of a variable

Here's my question:

I have a variable stands for 1 script, but I want to be able to change the type of the variable so that it won't be a variable for script1 but for script2 if something happened.

(answer with c# please)

Comment
Add comment · Show 8
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 perchik · Apr 01, 2014 at 06:53 PM 3
Share

... what?

avatar image F-N · Apr 01, 2014 at 07:01 PM 0
Share

what... what?

avatar image g0tNoodles · Apr 01, 2014 at 07:10 PM 0
Share

You need to explain what you want a bit better. But from what I can tell, you want something that is in script1 to be accessible in script2 if the player does something (scores a point for instance)?

If that is the case. You can do it a couple of ways. You could make the variable in script1 static and access it that from script2 that way or you could have a new variable in script2 which gets the component (variable) from script1.

avatar image F-N · Apr 01, 2014 at 07:13 PM 0
Share

Nope, I want 1 variable to be able to access script1, and if something happens, the variable must be able to access script2

avatar image Slobdell · Apr 01, 2014 at 07:32 PM 2
Share

Haha. This is an entertaining interaction. Variables don't "access" scripts. $$anonymous$$y only guess at what you're talking about would have been the same as gotnoodles.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Blaveloper · Apr 01, 2014 at 08:51 PM

First off, a variable doesn't access a script, a script accesses a variable.

Secondly, you cannot change the type of a variable without messing it all up (it's not like you can turn a string into a double and assume everything will work, for example).

With the pre-pre-pre-basics of programming behind us, we will now go on to very serious programming:

To access a variable in another script, make them public and static. Like this:

 public static int someVariable = 9000;

This variable exists in script1.cs. Now the really hard stuff comes! In script2.cs, type this:

 script1.someVariable;
Comment
Add comment · Show 6 · 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 F-N · Apr 02, 2014 at 10:47 AM 0
Share

You can access a script by variable like:

 private Script1 script; 

what I want then is to be able to access Script2 with it if something happens as well, like:

 private bool exampleBool;
 
 void Start(){
      script = GameObject.Find("player").GetComponent<Script1>();
      if(exampleBool == true){
          script = GameObject.Find("player").GetComponent<Script2>(); //doesnt work
      }
 }
avatar image Blaveloper · Apr 02, 2014 at 01:22 PM 0
Share

$$anonymous$$ay I ask you WHY you do it like that? Your script makes no sense.

Just do however I did it and you should be safe. Unless you're not looking for a variable to be accessed in another script.

avatar image whydoidoit · Apr 02, 2014 at 01:51 PM 1
Share

@F.N Right well you can do that if both scripts inherit from something that is a common base or both implement the same interface. Perhaps you could explain what you want to do with these scripts? Call similar functions on them? Access similar variables?

avatar image F-N · Apr 02, 2014 at 06:28 PM 0
Share

@whydoidoit Yea, I have 2 monsters with monster1: script1 attached and monster2: script2 attached. The variables that I have to access in the 2 scripts are mostly the same.

avatar image whydoidoit · Apr 03, 2014 at 04:10 PM 1
Share

So I think you need to make a base class with the shared variables in and then declare your variable with that base type...

   public Shared : $$anonymous$$onoBehaviour {
       public string commonVariable;
       public int anotherVariable;
   }

    public Script1 : Shared {
    }

    public Script2 : Shared {
    }

    public Shared script;
Show more comments
avatar image
0

Answer by FinKone · Apr 01, 2014 at 08:28 PM

Make the same type of variable in script 2, access script 1 in script 2, replace script 2 with 1s value / data and its stored. You can also send it to script 2 from 1 if you want it to happen at a certain point. Depends how when and why you want to back it up or duplicate it. Look into GetComponent to accomplish this type of thing, or acess th script directly in code....

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 F-N · Apr 03, 2014 at 02:26 PM 0
Share

Could you please give a example of that?

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

28 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

Related Questions

Change gameobject tag when player collides with another gameobject 1 Answer

Is changing the color of GUIText unavailable in the Free Edition? 1 Answer

Change Text of GUI Button from Script 2 Answers

script help gameobject to playercar 1 Answer

How do I keep changes I make to parameters while game is running live? 2 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