- Home /
Value to 0 on collision
Hi,
Very simple problem which I can't seem to solve and I haven't got the slightest idea why this does not work.
I have a variable called speed in one script
When my player collides, I want the speed to be 0, so it stops moving. I have 3 objects moving at different speeds using the same code (speeds are set in the inspector)
bgMove.speed = 0f;
The above code doesn't seem to do anything, and it is under OnCollisionEnter2D()
Am I doing it wrong?
Thanks
Your OnCollisionEnter is working? Can you have access that variable?
These are my 2 functions that I use.
hgmove is another script that I use with a simple variable speed
void OnCollisionEnter2D(Collision2D other)
{
collider2D.enabled = false;
Die ();
}
void Die()
{
bg$$anonymous$$ove.speed = 0f;
isDead = true;
}
Did you mistype hgmove and meant to say bg$$anonymous$$ove, because if bg$$anonymous$$ove is on a different script that is probably your problem.
Use Debug.Log statements to check what is being called. Is your collision actually happening?
Answer by meat5000 · Jun 22, 2014 at 10:41 PM
To hazard a guess, you need to 'Find' the relevant gameObjects and use GetComponent to access, or cache, bgMove.
Once it is cached you can then access the speed variable with the dot operator through your instance
bgMoveInstance.speed = 0f;
I am using GetComponent to access the variable. I think I have found my problem. The bg$$anonymous$$ove variable is run in Update().
public float speed;
void Update() {
transform.position += (Vector3.left * speed) * Time.deltaTime;
}
Do you mean making it like this?
public float speed;
void Start() {
speed = 0.5;
}
void Update() {
transform.position += (Vector3.left * speed) * Time.deltaTime;
}
I'm pretty weak in C# so bear with me :)
private gameObject bgObject = new GameObject;
private bg$$anonymous$$ove bg$$anonymous$$oveInstance = new bg$$anonymous$$ove;
void Start()
{
bgObject = GameObject.Find("Object-with-bg$$anonymous$$ove-on-it");
bg$$anonymous$$oveInstance = bgObject.GetComponent(bg$$anonymous$$ove);
}
void Update()
{
Debug.Log(bg$$anonymous$$oveInstance.speed)
}
I do apologise if its badly hashed nonsense but I'm sure you get the idea of what I'm doing here.
Find object with script you are trying to access and store it.
Use GetComponent to retrieve the script and store it in variable of the same type as your script. ($$anonymous$$y usage could be inaccurate there)
You can then use scriptInstance.variable
to access you variable as if its in the same script :)
This looks like it should work. I would use the Unity drag feature for this ins$$anonymous$$d of find function but should do the same job.
Aye, I usually use FindWithTag, as it's cheaper than Find. I avoid Drag to $$anonymous$$imise NullRefs for whatever reason :) Sometimes can't be avoided though.
Your answer
Follow this Question
Related Questions
Reference to another script not working, not throwing any errors 0 Answers
Javascript score problems whilst referencing scripts. 2 Answers
Object reference not set to an instance of an object 1 Answer
how to call the script of an object being collided with a trigger2D 1 Answer
Get Object ID upon collision. 1 Answer