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 Hydraxia · Jul 10, 2013 at 11:37 AM · guitoggle

Change something in a script with other script

Hello!

( Sorry for the bad english in this entire text )

I have a plane ( water ) that raises by time. This is the script for the raising water:

 using UnityEngine;
 using System.Collections;
 
 public class WaterRise : MonoBehaviour {
 
 public Vector3 raisespeed = new Vector3(0,5.0f,0);
 
 // Use this for initialization
 void Start () {
       
 }
 
 // Update is called once per frame
 void Update ()
 {
     //    WaitForSeconds(5);
 transform.Translate(raisespeed * Time.deltaTime);
 }
 }


This is the WaterRise script. And it works. Now i have an other script, the MainMenu script. These are all written in c#. So, i have a toggleabble ''radio button'' in the MainMenu script. I want to increase the value of ''raisespeed'' ( so the plane moves faster ). How can i do this? I can't figure out how to change the values of a script with other script. I want to change the Vector3(0,5.0f,0) This is the script for the MainMenu ( i know its not actually a main menu ):

 using UnityEngine;
 using System.Collections;
 
 public class MainMenu : MonoBehaviour {
 
     private bool fastmode = false;
 
     void OnGUI () {
         fastmode = GUI.Toggle (new Rect (25, 25, 100, 30), fastmode, "Fast mode");
     }
     void Update () {
         if (fastmode == true) {
          WaterRise = GetComponent("WaterRise") as WaterRise;
          WaterRise.do public Vector3 speed = new Vector3(0,5.0f,0);
         }
     }
 }



This code is a totally fail now.

Thanks for helping!

Comment
Add comment · Show 4
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 Eugenius · Jul 10, 2013 at 11:40 AM 1
Share

Are both scripts attached to the same GameObject?

avatar image Hydraxia · Jul 10, 2013 at 11:43 AM 0
Share

Not, because the WaterRise is attached directly to the plane that raises ( the water ) And the other is attached to the camera. It's important that these are not in same scene. One is in the $$anonymous$$ain$$anonymous$$enu scene and the other is in the $$anonymous$$ap scene.

I have the WaterRise script with the plane in the $$anonymous$$ap scene. And the $$anonymous$$ain$$anonymous$$enu script in the $$anonymous$$ain$$anonymous$$enu scene, attached to camera.

avatar image amphoterik · Jul 10, 2013 at 11:57 AM 1
Share

Oh, I didn't notice that they weren't in the same scene. Let me modify my answer.

avatar image Hydraxia · Jul 10, 2013 at 12:02 PM 0
Share

No problem, it's my fault, because i didn't wrote that at beginning.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by amphoterik · Jul 10, 2013 at 12:02 PM

EDIT: I didn't notice they were in different scenes. To fix this you will need to change your MainMenu to say something like:

 void Start()
 {
     PlayerPrefs.SetInt("raisespeed", 5);
 }
 void OnGUI () {
     fastmode = GUI.Toggle (new Rect (25, 25, 100, 30), fastmode, "Fast mode");
     if(GUI.changed && fastmode == true)
         PlayerPrefs.SetInt("raisespeed", 10);
     else if(GUI.changed && fastmode == false)
         PlayerPrefs.SetInt("raisespeed", 5);
 }
 void Update () {
 
 }

Then change your waterrise script to be:

 public Vector3 raisespeed;
  
 // Use this for initialization
 void Start () {
      raisespeed = new Vector3(0, PlayerPrefs.GetInt("raisespeed"), 0);
 }

That will do it for you.

More info on playerprefs can be found here: http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html

Comment
Add comment · Show 9 · 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 Hydraxia · Jul 10, 2013 at 01:04 PM 0
Share

Thank you, but i don't know why it still doesn't work. It now says: GetInt can only be called from the main thread. What is my main thread? ( I set up the scenes in the build settings, still don't work ) Thank you, sorry for disturbing

avatar image amphoterik · Jul 10, 2013 at 01:47 PM 1
Share

Can I see your code now? I believe that your code might be outside of a function or in the wrong scope (thats what generally causes this problem)

avatar image amphoterik · Jul 10, 2013 at 03:29 PM 1
Share

Here is your problem public Vector3 raisespeed = new Vector3(0,PlayerPrefs.GetInt("raisespeed"),0); Put the code in your waterRise script like I showed you in my answer:

 public Vector3 raisespeed;
  
 // Use this for initialization
 void Start () {
 raisespeed = new Vector3(0, PlayerPrefs.GetInt("raisespeed"), 0);
 }
avatar image amphoterik · Jul 10, 2013 at 04:59 PM 1
Share

Correct. That is what I am saying. Remove the line you just posted.

avatar image amphoterik · Jul 10, 2013 at 05:17 PM 1
Share

Cool, glad it helped. Feel free to select as answer and upvote (if you can) you will get karma for it too.

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

17 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

Related Questions

How to create a gui button inside an if statement? 1 Answer

Inputing an Equation 0 Answers

My own GuiSkin on the script 2 Answers

Simple Backpack "Pick up and drop" 1 Answer

GUI button not showing up 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