- Home /
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!
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.
Oh, I didn't notice that they weren't in the same scene. Let me modify my answer.
No problem, it's my fault, because i didn't wrote that at beginning.
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
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
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)
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);
}
Correct. That is what I am saying. Remove the line you just posted.
Cool, glad it helped. Feel free to select as answer and upvote (if you can) you will get karma for it too.
Your answer
Follow this Question
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