im trying to dissable a certain script in a diffrent game object. and it gives me this error.
““scriptName” is a ’ type which is not valid in the given context”
iv looked at multiple forums and cant figure out what is going on.
this is my code
public GameObject Scene2;
Scene2.GetComponent(ParallaxBackground).SetActive(false);
---------------------------------------------------------------------------------------------------------------------------------------------------
(i dragged in a game object in the unity editor idk if that is what its called)
If you’re going for the first example, my guess is you just have it incorrect. I honestly can’t say for certain as I only use the generic version.
That error message doesn’t match your code. Please copy and paste the full error message and the script that the error message is referencing
ok
“‘ParallaxBackground’ is a type, which is not valid in the given context”
just a heads up my code sucks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenSwitch : MonoBehaviour
{
//tells the game where to send the player if gone to far to the left
public Vector3 leftExitPosition;
//tells the game where to send the player if gone to far to the right
public static Vector3 RightExitPosition;
//position for screenbox move screen1-2
public Vector3 Screenbox1;
public Vector3 Screenbox2;
//------------------------------------------------------------------------------------------------------------------------
//calls scene1
public GameObject Scene1;
//calls scene 2
public GameObject Scene2;
public int SceneNumber = 1;
private void Awake()
{
}
// Start is called before the first frame update
void Start()
{
//sets the left exit position
leftExitPosition.Set(-63f, 0f, -3f);
//sets the position scene 1-2
Screenbox1.Set(-43f,4f,-100);
Screenbox2.Set(-3.805568f, 3.950633f, -100);
}
// Update is called once per frame
void Update()
{
if(SceneNumber == 1)
{
Scene1.SetActive(true);
Scene2.SetActive(false);
}
if (SceneNumber == 2)
{
Scene2.SetActive(true);
Scene1.SetActive(false);
}
}
//collider trigger function
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.name == “exit colider left scene 1”)
{
GameObject.Find(“screenbox”).transform.position = Screenbox1;
SceneNumber = 2;
transform.position = new Vector3(-22f, -1f, -3);
}
if (other.gameObject.name == “exit colider right scene 2”)
{
GameObject.Find(“screenbox”).transform.position = Screenbox2;
Scene2.GetComponent(ParallaxBackground).SetActive(false);
SceneNumber = 1;
transform.position = new Vector3(-17.2f, -1f, -3);
}
}
//collider exit function
private void OnTriggerExit2D(Collider2D other)
{
//exit collider left
if (other.gameObject.name == “exit colder left scene 1”)
{
}
}
}
A little background on this script cause my code is terrible and can barely be red.
its a screenswitcher for screenboxes, i use this method of switching cause i cant find a viable solution that isnt messy. im having issues with my parallax script so im trying to call it and dissable it temporaraly in certain spots. but im pretty sure this fix wont work, but its all i can think of atm
ok ill check it out. see if that is whats wrong
You need to use one of these forms:
Scene2.GetComponent<ParallaxBackground>()
Scene2.GetComponent(typeof(ParallaxBackground))
Scene2.GetComponent("ParallaxBackground")
1 Like
oh ok makes sense (not to me but im sure it makes sense to sombody)
Also if you can’t add it look if the script and the code have the same name for example
c# script is called: ScreenSwitch
and in the code its called: public class ScreenSwitch : MonoBehaviour
Maby thats youre problem ? best regards Xavier