- Home /
Setting bool from other script for specific gameobjects (while they're using same movement script)
Greetings. o/
After numerous hours and days spent on various forums and youtube videos now I gotta raise my hands for total surrender.
I'm making a simple gravity based game. Where you're in control of 10 differently shaped objects all at the same time. Goal is trying to reach certain safespot with them and using only three directions: left,up and right using only mouse buttons and keyboard.
This game project is mainly for learning how to handle basic situations on Unity. :) My test-game's other scenes work flawlessly (Where scripts are passing variables from other scripts, turning objects when certain variable is fulfilled, teleporting, checking if tag is allowed to enter etc.)
But! Now I'm having a hard time with this one little boolean variable which keeps affecting all gameobjects with same script instead of affecting independently.
Desperately trying to find solution and NOT using spaghetti code (if statement for each gameobject along with own personalized c# movement script) for no-no solution.
Any ideas and examples are more than welcome. Added comments in scripts for making it a little bit easier to follow/read.
Those headaching scripts:
HiiriPois.cs (MouseOff) is attached to empty large gameobject with Mesh Renderer off for acting as trigger for incoming gameobjects.
HiiriPois.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HiiriPois : MonoBehaviour
{
// for identifying gameobjects entering trigger instead of numerous if statements
private string EsineLista = "|Esine1|Esine2|Esine3|Esine4|Esine5|Esine6|Esine7|Esine8|Esine9|Esine10|";
public bool VainHiiri = false;
void OnTriggerStay(Collider collider)
{
if (EsineLista.Contains(string.Format("|{0}|", collider.tag)))
{
VainHiiri = true; // this is working in inspector.
// Tick appears on it but this just
// doesn't pass it to Movement script
// for that certain gameobject.
}
}
void OnTriggerExit(Collider collider)
{
if (EsineLista.Contains(string.Format("|{0}|", collider.tag)))
{
VainHiiri = false; // inspector tick disappears like it should.
}
}
}
Movement.cs is attached to every controllable gameobject.
Movement.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
// Getting that certain bool value for each gameobject
public bool VainHiiri;
public HiiriPois RatOff;
// For Controlling multiple GameObjects
public float thrust = 20f;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
// RatOff = GetComponent<HiiriPois>().VainHiiri;
// Just one of desperate tests for getting that
// boolean status from HiiriPois trigger
VainHiiri = GameObject.Find("PoistaKiippari").GetComponent<HiiriPois>().VainHiiri;
// PoistaKiippari is a gameobject with Mesh
// Renderer off. Holding HiiriPois script.
}
void FixedUpdate()
{
if (RatOff.VainHiiri == true) // Can only be activated/deactivated manually
// through inspector for each gameobject
// getting that disabled keyboard control
{
Debug.Log("Only Mouse control is on!"); // for debug purposes only
}
// move objects to left
if (Input.GetMouseButtonDown(0) || Input.GetKey("a") && RatOff.VainHiiri == false)
{
rb.AddForce(-5, 0, thrust * Time.deltaTime, ForceMode.VelocityChange);
}
// move objects to right
if (Input.GetMouseButtonDown(1) || Input.GetKey("d") && RatOff.VainHiiri == false)
{
rb.AddForce(5, 0, thrust * Time.deltaTime, ForceMode.VelocityChange);
}
// move objects to up
if (Input.GetMouseButtonDown(2) || Input.GetKey("s") && RatOff.VainHiiri == false)
{
rb.AddForce(thrust * Time.deltaTime, 5, 0, ForceMode.VelocityChange);
}
}
}
Answer by Anaxis_Studio · Sep 14, 2019 at 08:31 PM
HiiriPois class is attempting to change the VainHiiri boolean of the GameObjects Movement script without using the correct reference.
GameObject A has a script (Script 1). This script is trying to access a boolean from (Script2) on each GameObject (GO1, GO2, GO3, etc).
public GameObject myObject1;
myObject1.GetComponent<Movement>().VainHiiri = true;
So your HiiriPois class needs to hold a reference to these objects, and get their respective component. But you're doing it a slightly different way than above so maybe try this?
OnTriggerEnter(Collider other)
{
other.GetComponent<Movement>().VainHiiri = true;
}
Answer by vainoharha · Sep 15, 2019 at 08:35 PM
I'll take off my hat (if I ever use one) to salute you \o/
Your simple yet excellent explain along with example (I learn best from examples) did
it's magic and GetComponent have finally opened to me. :)
Last example of yours did the trick so easily with only a little bit of modification and
now I could get rid of pile of meaningless tests trying to get a hang of this problem.
Yet again. Thank you so much!