All I would like to know is how to call other game objects from other script. Here’s a part of script I have.
rivate TrackableBehaviour mTrackableBehaviour;
public GameObject Monster; // The enemy prefab to be spawned.
public Vector3 Zone1V;
public Vector3 Zone2V;
public Vector3 Zone3V;
public Vector3 Zone4V;
public Vector3 Zone5V;
public Collider Zone1;
public Collider Zone2;
public Collider Zone3;
public Collider Zone4;
public Collider Zone5;
GameObject SpawnedM;
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler (this);
}
I have another script and all I want to call or access is the public game object Monster.
Your script has some issues such as you returning before your debug.log, so it will never debug.log.
You also dont need all those “else” (unless if you have it there cuz you aint done writing the code).
Also, if the first if statement fails, all other if statements will not be read. I am assuming you want the code to be more like this…
Click for code
using UnityEngine;
using System.Collections;
public class Zone : MonoBehaviour
{
public Vector3 Zone1V;
public Vector3 Zone2V;
public Vector3 Zone3V;
public Vector3 Zone4V;
public Vector3 Zone5V;
GameObject Monster;
GameObject SummonedMonster;
void Start()
{
}
void OnTriggerEnter (Collider other)
{
if (other.transform.name == "MZ1T")
{
}
else if (other.transform.name == "MZ2T")
{
}
else if (other.transform.name == "MZ3T")
{
}
else if(other.transform.name == "MZ4T")
{
}
else if(other.transform.name == "MZ5T")
{
Debug.Log("Monster Zones Full");
return;
}
}
}
There can be many ways to handle this, such as switch case statements.
As for “For Some reason it doesn’t work.” what have you tried so far?
What exactly are you trying to access? You say you want the GameObject Monster; on the Zone class to reference your actual monster gameobject out there somewhere in the hierarchy?
Tell me your scenario so I can have a better idea on things.
The game object isn’t in the hierarchy. It’s Just a reference to Instantiate a prefab. Here’s the code with the Monster.
using UnityEngine;
namespace Vuforia
{
public class Spawn : MonoBehaviour,
ITrackableEventHandler
{
private TrackableBehaviour mTrackableBehaviour;
public GameObject Monster; // The enemy prefab to be spawned.
public Vector3 Zone1V;
public Vector3 Zone2V;
public Vector3 Zone3V;
public Vector3 Zone4V;
public Vector3 Zone5V;
public Collider Zone1;
public Collider Zone2;
public Collider Zone3;
public Collider Zone4;
public Collider Zone5;
GameObject SpawnedM;
public Zone ZoneScript;
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler (this);
}
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
ZoneScript.OnTriggerEnter();
}
else
{
}
}
}
}
And I haven’t finished the code and if the “if” statement fails its ok because it will go to else.
Yes. Thank You I appreciate your skills. I have one more question please help with this small thing. I want to be able to execute a function in the Zone script using the Spawn script. Its the OnTriggerEnter function. But it says it has 0 Arguments.
Well, the on trigger method is really more so meant for the physics engine to handle, though Im sure you can still call it yourself, you just need to assign it a collider in the parameter. However, I advise to not use OnTriggerEnter for this since it might cause weird results.
So, you are probably doing this… ZoneScript.OnTriggerEnter(); but you need to do ZoneScript.OnTriggerEnter(enter some collider here);
Again, you shouldnt really call any of the OnTriggerxxx or OnCollisionxxx methods. Bad practice imo.
Tell me what it is you are trying to do and maybe I can give some better information =).
Why do you have these zones? Are you trying to limit how many spawns there can be?
From start to finish, what is your goal with these 2 scripts?
I am using the Vuforia Arguemented reality script. Heres the original script.
/*==============================================================================
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Qualcomm Connected Experiences, Inc.
==============================================================================*/
using UnityEngine;
namespace Vuforia
{
/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour,
ITrackableEventHandler
{
#region PRIVATE_MEMBER_VARIABLES
private TrackableBehaviour mTrackableBehaviour;
#endregion // PRIVATE_MEMBER_VARIABLES
#region UNTIY_MONOBEHAVIOUR_METHODS
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
#endregion // UNTIY_MONOBEHAVIOUR_METHODS
#region PUBLIC_METHODS
/// <summary>
/// Implementation of the ITrackableEventHandler function called when the
/// tracking state changes.
/// </summary>
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
OnTrackingFound();
}
else
{
OnTrackingLost();
}
}
#endregion // PUBLIC_METHODS
#region PRIVATE_METHODS
public void OnTrackingFound()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Enable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}
// Enable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
}
public void OnTrackingLost()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Disable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = false;
}
// Disable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
}
#endregion // PRIVATE_METHODS
}
}
I am making a yugioh game and there can only be five spawn points. See ScreenShot. And I have changed the script to this
using UnityEngine;
namespace Vuforia
{
public class Spawn : MonoBehaviour,
ITrackableEventHandler
{
private TrackableBehaviour mTrackableBehaviour;
public GameObject Monster; // The enemy prefab to be spawned.
public Vector3 Zone1V;
public Vector3 Zone2V;
public Vector3 Zone3V;
public Vector3 Zone4V;
public Vector3 Zone5V;
public Collider Zone1;
public Collider Zone2;
public Collider Zone3;
public Collider Zone4;
public Collider Zone5;
GameObject SpawnedM;
public Zone ZoneScript;
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour> ();
if (mTrackableBehaviour) {
mTrackableBehaviour.RegisterTrackableEventHandler (this);
}
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
ZoneScript.OnTriggerEnter();
}
else
{
}
}
}
}
so that i can fit it so that when the behavior is tracked the colliders function will execute and that will limit the spawn cards.
The reason I’m using trigger colliders was so when a object or monster is in zone 1 it will spawn in zone 2 or if a monster was in zone 1 and 2 it will spawn in zone 3. And so on. That’s the point of the collider.
It might be easier to just use a dictionary to keep track of things. Something like Dictionary<Vector3, bool> isZoneEmpty
The vector3 is the zone vector and when you put a monster inside a zone, you mark the bool to false, showing it isnt empty.
You can loop through the dictionary to check for an available spot.
Thanks for the advise and your help. But I find it better to learn when asking questions and devote myself to a project and learn from the project. Thats how I like to learn. I’m used to c++ in Robotics but this is different.
Asking questions is great, its just that you are probably better off asking google and not us about a whole language from start to finish. The time it would take for someone to reply to a simple question such as “how to use a dictionary” compared to just googleling it would make you learn at a slow pace, and many times people would probably just link you to something explaining it anyways.
We are coders afterall, writing something down that was already written is against our code =).
Every question you ask is consuming the time of others here on the board. Everyone prefers asking questions, it’s really very little effort at at all on your part, so who wouldn’t love it? However, we learn to find things out on our own- studying, reading/watching tutorials, navigating through demos, so as to not ask too many fundamental questions unnecessarily. This board is for people having technical problems that they just can’t seem to figure out- it should be the final resort when internet searches and the Unity documentation have both failed to turn up a viable solution in a reasonable amount of time.
Please, be considerate of others and don’t abuse the support forum.