Can I create a light source via script in Unity? - c#

I'm trying to write a little mod for Kerbal Space Program a game which uses Unity. I've got a class that is a child of MonoBehaviour which loads correctly and all. Part of this mod involves creating a new light source in the current scene. My question is as follows: is it possible for me to create a new Unity light source in the current scene using a script rather then the unity engine scene editor (which I obviously don't have access too as a modder).
Example of the sort of thing I'm looking for (I know it won't actually look anything like this but just to give you an idea of what I need)
UnityEngine.getCurrentScene().createObject(new Light(pos, direction, color, strength));

Create the gameobject and add the light component:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Start() {
GameObject lightGameObject = new GameObject("The Light");
Light lightComp = lightGameObject.AddComponent<Light>();
lightComp.color = Color.blue;
lightGameObject.transform.position = new Vector3(0, 5, 0);
}
}

Related

Player does not teleport when using PUN2 network

Player does not teleport when using PUN2 network. Basically, I do not know how to add the function to get the player to teleport while being on a network. I am only just getting into GameDev and I am trying to learn as much as possible while creating a game at the same time. So, I wanted to add portals to teleport the players to and from different parts of the map, but it doesn't work while being logged into a network unfortunately. Thank you for any assitance in solving this issue its been bothering me for a few days now lol. Here is the c# script that I use to teleport the player and again remember I know it needs to be using some type of network code inside I just do not know how to add it unfortunately. Thanks!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleport : MonoBehaviour
{
public Transform teleportTarget;
public GameObject thePlayer;
void OnTriggerEnter(Collider other)
{
thePlayer.transform.position = teleportTarget.transform.position;
}
}
I'm not sure if this has anything to do with your issue, but when using Pun2, you probably want to include it
using Photon.Pun;
using Photon.Realtime;
and then you may need to check which player has hit the trigger. I hope this works, but please provide a few more details on what exactly isn't working.

I started coding a few days ago and I had trouble with this message "cant add script component because the script class cannot be found" (Unity)

I created a script in Unity (using Visual Studio Code). Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Propellermovement : MonoBehaviour
{
private GameObject propeller;
public float Propellerspeed;
void Update()
{
transform.Rotate(Vector3.forward * Propellerspeed * Time.deltaTime);
}}
I don't know if someone knows how to help me.
I created new scripts and it still didn't work. I can't add the component to an object, and I don't know if it is because it's an object into an object (sorry English its not my first language).
As noted in the comments, your file name must exactly match the name of your class. You need to have your file named exactly "Propellermovement.cs". If you change the name of your class, you must also change the name of your CS file in order for Unity to properly recognize it.

Unity: Use Inherited properties component or Inspector Injected components

I'm new to Unity but have development experience. I'd like to know whether I should be using the inherited properties in my MonoBehaviour scripts, or if I should be injecting everything from the inspector instead.
I'm following a tutorial that has suggested writing some code like this:
public class BirdScript : MonoBehaviour
{
public Rigidbody2D myRigidbody;
So that the Inspector can inject the component's Rigidbody2D into it:
When playing with the code, I noticed that MonoBehaviour extends Component and I can do this.rigidbody2D instead. I got told off by Intellisense for this, but it suggested instead using: GetComponent<Rigidbody2D>().
Using this, instead of my code reading:
void Update()
{
myRigidbody.velocity = Vector2.up * 10;
}
I can write:
void Update()
{
GetComponent<Rigidbody2D>().velocity = Vector2.up * 10;
}
But which is better? I'm thinking GetComponent<Rigidbody2D>() because it makes sure that I'm directly referencing the current rigid body that my script is attached to, instead of requiring me to apply things in the Inspector, but maybe this is too inflexible for Unity? In general, the style seems more lenient towards making things public instead of private for the sake of the Inspector.
Any advice on some best practices I can use to make these kinds of decisions?

Object Reference Not Set to an Instance - Unity2D

I have this simple code to change the sprite of an image everytime I click a button.
using UnityEngine;
using UnityEngine.UI;
public class SampleChange : MonoBehaviour {
public Sprite sampleSprite;
public Image sampleImage;
public void Start()
{
sampleImage = GetComponent<Image>();
}
public void changeColor()
{
sampleImage.gameObject.GetComponent<Image>();
sampleImage.sprite = sampleSprite;
}
}
I attached this script to an EmptyGameObject and Loaded the function on the Button that is parented on a Canvas alongside the Image. I already also placed the Image and Sprite objects in the inspector:
Inspector Settings
When I run the game and click the Button, it gives me this error:
NullReferenceException: Object reference not set to an instance of an object
SampleChange.changeColor () (at Assets/Scripts/SampleChange.cs:18)
The cs:18 is the sampleImage.sprite = sampleSprite;. I really don't know why it's not working.
OK simple,
public Image sampleImage;
that means
you will set "sampleImage" variable in the inspector, in the editor, before you hit Play
But this one ..
sampleImage = GetComponent<Image>();
means
you will set "sampleImage" variable in code when the scene is running.
You have to sort it out and do it "one way or the other".
Suggest you use the first method while U learning.
(If you do use the second method, the "Image" must actually be on the game object which is holding the script in question. If you struggle with that, I would urge you to ask a separate question, or just study up on the basics using Unity tutorials.)
Cheers
It appears that you have no constuctor defined for your class and that you are just trying to call the changeColor() method as if it were a static function of the class. You need to construct objects of your class and then call the methods you defined on those objects, not on the class itself.

How to set different classes at runtime in Unity

I am making a game in Unity C# where the character will have different characterstics and movement functions in each scene. So I am trying to have different scripts for a player in different scenes, while all of them inheriting from the same base class. The definition will be something like below
public class PlayerScript1 : PlayerBase { /* for scene 1*/
public void Move(){
/* my movement code*/
}
}
Similarly, I have a separate class for Scene2 as
public class PlayerScript2 : PlayerBase { /* for scene 2*/
public void Move(){
/* my movement code*/
}
}
Now the problem is, my other scripts, like HealthScript,ScoreScript etc they do not change with scene. But they do access PlayerScript1 script. And thats why, I have the PlayerScript1 declaration in them. Like below:
public class HealthScript : MonoBehaviour {
PlayerScript1 playerScript;
void Start(){
/*accessing the script attached to game object*/
}
}
So how can I have my Health Script access different instances of my PlayerScript based on the scene? I know I could use delegates to call different methods in runtime, but how can I do the same with classes?
So how can I have my Health Script access different instances of my PlayerScript based on the scene?
Well first, you'll want to declare that object as of Type PlayerBase as you will be unable to assign an instance of PlayerScript2 to a variable of type PlayerScript1: those classes might inherit from the same parent, but they are not the same and you cannot convert from one to the other.
After that you will need to search for the player object in the scene, something like...
void Start(){
playerScript = GameObject.Find("Player").GetComponent<PlayerBase>();
}
Assuming, of course, that PlayerBase extends MonoBehaviour. If it doesn't you can't get a reference this way (as it won't exist in the scene at all). Additionally if you want this health object to persist from scene to scene, you need to call DontDestroyOnLoad() for it (as well as remembering that if you don't start testing from Scene 1 where this object is, it won't exist at all, or if you have a copy in every scene, you'll have duplication problems).
Someone answered to my question on the Unity forum which cleared all my doubts:
The key was using the below line in my HealthScript :
PlayerBase player = (PlayerBase)FindObjectOfType(typeof(PlayerBase));
http://answers.unity3d.com/answers/1348311/view.html

Categories