- Home /
how to set a value in one script and call it in another? (c#)
here is the issue, i want to have 3 things happen, thus 3 scripts (if it can be done with fewer, that would be great). and it is not working like it should.
have the player trigger the
Trap
have the trap be triggered by the
TrapTrigger
and start to move (an object)have the player restart the level if they collide with the
Trap
Tthe issue is, i cannot get it to work, the TrapTrigger
does not activate the Trap
to move. the Trap
just starts on its own. I know this is because of the start function, but i can't use an update function with an IEnumerator
. I was trying to do an if
statement within Start
to check if the Trap
has been set active, but that doesn't seem to be working. here are the 3 scripts.
TRAP:
using UnityEngine; using System.Collections; public class Trap : MonoBehaviour { public Vector3 point2; public Trap trap; IEnumerator Start() { if (trap == enabled) { Vector3 point1 = transform.position; while (true) { yield return StartCoroutine(MoveObject(transform, point1, point2, 2.0f)); yield return StartCoroutine(MoveObject(transform, point2, point1, 2.0f)); } } } IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time) { float i = 0.0f; float rate = 1.0f / time; while (i < 1.0f) { i += Time.deltaTime * rate; thisTransform.position = Vector3.Lerp(startPos, endPos, i); yield return null; } } }
TRAP TRIGGER:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class TrapTrigger : MonoBehaviour { public Trap trap { set; get; } void OnTriggerEnter(Collider other) { if (other.tag == "Player") { trap.enabled = true; } } }
RESTART:
using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; public class ColRestart : MonoBehaviour { void OnTriggerEnter(Collider other) { if (other.tag == "Player") { SceneManager.LoadScene("Level_03"); } } }
all i want is the player to collide with the TrapTrigger
- the TrapTrigger
to start the Trap
movement - and if they player enters the Trap
, it triggers the restart script ColRestart
.
any help is welcomed. thank you.
P.S. i did post this question before and though it was working, but after some testing it did not work as intended.
Please let me know if you need any more info.
NOTES
I was thinking to try having the
TrapTrigger
on theTrap
object, but that would defeat the purpose, as the game would just simply restart because of the restart script.Think of it like an dart trap in a puzzle game, like tomb raider, where you step on a tile, and then darts just shoot back and forth, and if the player gets hit they take damage. in this case if they get hit by the object the game restarts.
This is the error that comes up when I enter the
TrapTrigger
object collider.NullReferenceException: Object reference not set to an instance of an object TrapTrigger.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/TrapTrigger.cs:14)
Answer by Trevdevs · Jun 23, 2018 at 02:14 AM
I'm actually suprised that you used and Ienum with the Start function I didn't even know that was possible but what I would do is simplify it to 1 script yep 1 that's it.
Somethings to keep track of are A. Make sure your script is on a object with a collider set as isTrigger or it won't be called. B. Why I hate using tags I always forget to set them on the object in the inspector so make sure your player is set with a player tag. Finally make sure your player has a rigidbody as well or the trigger wont run.
C. this is just me being a whiney programmer you don't gotta do this it would just be less lines of code. Don't program an animation create an animation and play it in code (MoveObject)
Here is a good starter for you
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(BoxCollider))]
public class Trap : MonoBehaviour
{
public Vector3 point2;
public bool isTrapEnabled;
public bool shouldResetPlayer;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
if(shouldResetPlayer)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
else
{
StartCoroutine(PingPong());
}
}
}
IEnumerator PingPong()
{
//Move object back and forth
yield return null; //waits till next frame
}
}
First, thank you! Second: im confused, how does the object move back and forth ? you want me to use an animation? also, how is the animation starting? i want it to start when the player "steps on" a trap trigger. in the code you have here isn't it stating that if the player does not walk into it the trap to run the back and forth motion? which makes sense, because i don't want it to continue moving after the player has collided with it, but how does the trap start to move? I get that I can put the restart script and the trap script together like this.
using UnityEngine;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
public class Trap : $$anonymous$$onoBehaviour
{
public Vector3 point2;
public Trap trap;
IEnumerator Start()
{
if (trap == enabled)
{
Vector3 point1 = transform.position;
while (true)
{
yield return StartCoroutine($$anonymous$$oveObject(transform, point1, point2, 2.0f));
yield return StartCoroutine($$anonymous$$oveObject(transform, point2, point1, 2.0f));
}
}
}
IEnumerator $$anonymous$$oveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
float i = 0.0f;
float rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name);
}
}
}
But, how can I have a trap, that is supposed to be a completely different object, far away from the moving trap, be in the same script without it being also a restart function, as the restart is connected to the collider of the object as a whole, correct? please advise. thank you
how to move an object back and forth would be a simple google search, I was just to lazy to code it lol https://forum.unity.com/threads/moving-an-object-back-and-forth-on-a-single-axis-automatically.235033/
Since i don't know what your trap is or how it moves but for instance lets just say you make an animation 2 keyframes one at its starting point and another 5 units to the right. Ins$$anonymous$$d of starting the coroutine and doing complex math you can just play the animation.
$$anonymous$$y code here will run when the player enters the trigger and depending on what "type" of trap it will either start moving or it will reset the player hence the boolean.
ok, i think i get what you mean. I just though not using a animation would be easier, but i can see that it actually may be more difficult not to use it. that link you provided is what i used for my script. so if i was going to an animation, i would just ignore all that, and just tell the animation to innisheate when the player enters a collider?
Your answer
Follow this Question
Related Questions
Problem About Get and Set Methods 0 Answers
c# setter/getters 1 Answer
Instantiation Wont Stop 1 Answer
Public variable hidden in the inspector 2 Answers
When to use SETters and GETters 2 Answers