Simple issue with calling external function

Hey guys,

I am so stuck on that simple one, could you help ?

For a VR project, I am using the SteamVR package and a teleport point which is used to jump to another scene.
I have seen in teleportpoint.cs (line 236) that you have to add some code to handle the loading of a scene.
I would like to do that using a function in another public script attached to my Player object.
Everytime I try to call that function, I get an error “The type or namespace 'Scene_Loader” could not be found.
Any idea why ? Is it related to the namespace directive at the root of TeleportPoint.cs ?

Here is the code I added :
Code (CSharp):

  • public void TeleportToScene()
  • {
  • if ( !string.IsNullOrEmpty( switchToScene ) )
  • {
    • if (!string.IsNullOrEmpty(start_position)) //a start position in the future scene is set
  • {
  • //set the start position in the future scene
  • Debug.Log("Switching to scene : " + switchToScene + ", with start position : " + start_position);
  • //SceneManager.LoadScene(switchToScene);
  • GameObject.Find(“Player”).GetComponent<Scene_Loader>().start_player_position = start_position; //check static value of setting in scene_loader script.

Thanks !!

Please post code using code tags, not as a numbered list.

That error usually means you forgot to put a “using” statement at the top of your file to tell the compiler what namespace to look in for the class in question. It could also mean that you spelled the name wrong or something like that.

Thanks for your answer.
I do not have a specific namespace defined in my Scene_Loader.cs file. Even if I add one, when I try to indicate using on top of the teleportPoint script, this does not work.
Basically, it is all about call an external function from that line 245. Could you have a look in the file attached and maybe see what is wrong ?
When I call this external function from other scripts, it works ! So why from there on line 245 it does not work ?
Thanks so much !

5164370–512255–TeleportPoint.cs (11.3 KB)

Find the file containing the Scene_Loader class.

Look for a namespace statement at the top of that.

Whatever that namespace is, either use it explicitly in your script (i.e., .GetComponent<WhateverNameSpace.SceneLoader>), or put a using statement at the top.

If you can’t find the Scene_Loader class, check your spelling, check the docs, find out where it is when you use it elsewhere by right-click go to declaration.

Dear Kurt, the Scene_Loader script I wrote myself and I did not mention any namespace.
If I try to access this function from another script/class it works, just from that TeleportPoint class it does not work… I am wondering :

  • Is it because the name of my class/script file is using an “_” ? (Scene_Loader)
  • the location of my script file in the project directory structure has an influence ? (SteamVR is a package downloaded from the asset store)
  • Is it because the class definition uses a colon ?
    The file header is :
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;

namespace Valve.VR.InteractionSystem
{
    //-------------------------------------------------------------------------
    public class TeleportPoint : TeleportMarkerBase
    {

Thanks !

Ah, that might be it. Unity compiles things in separate stages. Check here:

https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

Check specifically the “basic rule” thing, which I shall quote here in its entirety:

“The basic rule is that anything that is compiled in a phase after the current one cannot be referenced. Anything that is compiled in the current phase or an earlier phase is fully available.”

If the Scene_Loader is compiled in a later phase (see the chart at the link above) than where it is used, then it won’t be available to use. You would have to make some kind of runtime-supplied delegate to get information over to there, or pass messages back to the Plugins, for example.

Thanks Kurt !
I did move my scene_loader scripts to the SteamVR script folder and now works fine !
No idea how I can easily let it in the original folder and have compilation ok (could not find why this SteamVR scripts compile before my own scripts), but works for now !
Thanks again for the help !

1 Like

You’re welcome!

Well you sorta have to take a look at what you’re trying to do. At the point in the code you WANT to get that Scene_Loader, instead put all the information into some kind of structure or class, then invoke an event or call a delegate.

Outside of the plugins folder, you would reach down to that plugin and say “Hey, I wanna be notified when you do that stuff,” or “If you need information, call me.”

It might be a bit more complicated if you have to go back and forth or the transaction is complicated, but studying it with a mind to packaging it up into a delegate or event is probably the best way.

But hey, if this works, SHIP IT!! :slight_smile: