I am trying to change collidesWith paramater of an particle system inside of a script but i am getting this error:
Error CS1612 Cannot modify the return
value of 'ParticleSystem.collision'
because it is not a variable
My Code:
GameObject ammo; //Game object with ParticleSystem on it
public LayerMask desiredLayers;
private void Start()
{
ammo.GetComponent<ParticleSystem>().collision.collidesWith = desiredLayers;
}
Now my question is what is the correct way to change the layers of a particle system collide with.
Okay i figure it out, apparently ParticleSystem is a property.
And Unity have something special for ParticleSystem which uses Pointers so following code solved my problem:
var collidesWith = ammo.GetComponent<ParticleSystem>().collision.collidesWith;
collidesWith = desiredLayers;
Related
I want to attach an object to the OVRCameraRig and then use its position, which is offset from the rig.
However, my object is always static, irrespective of where the headset is.
This only happens with the OVRCameraRig. If I use a normal MainCamera I get the right data. But I'm not getting other aspects, like floor level, of the OVRCameraRig!
Is there some way to get the actual position of the OVRCameraRig?
Afaik the OVRCameraRig itself doesn't move.
What you probably want to get is the position of the centerEyeAnchor instead
// somehow get the reference e.g. using GetComponent
OVRCameraRig overCameraRig;
var position = overCameraRig.centerEyeAnchor.position;
Regardless of the value of usePerEyeCameras the centerEyeAnchor's position is always updated.
Try the following code to get the OVRCameraRig position using the centerEyeAnchor attribute.
using UnityEngine;
public class OVRCameraPosTest : MonoBehaviour {
[SerializeField] private OVRCameraRig overCameraRig;
void Start() {
Vector3 cameraPos = GetCameraPos();
Debug.Log("Camera Position: " + cameraPos);
}
Vector3 GetCameraPos() {
// Remove this line if you are refering the OVRCameraRig component
// through the inspector to the script.
overCameraRig = GameObject.Find("OVRCameraRig").GetComponent<OVRCameraRig>();
return overCameraRig.centerEyeAnchor.position;
}
}
I used to have the following particle system that worked.
// In the inspector I drag in the leaf particle system.
public ParticleSystem LeafStormParticleSystem;
private IEnumerator activate(float ActivateFor) {
//Change number of particles to 150
LeafStormParticleSystem.maxParticles = 150;
// LeafStormParticleSystem.
var newEmission = LeafStormParticleSystem.emission;
var rate = newEmission.rate;
rate.constantMin = 20;
rate.constantMax = 21;
newEmission.rate = rate;
Now as you can probably tell, this simple increases the number of particles. Now, this used to work and probably doesn't because of the new particle system I keep reading about.
However on this new particle system I have a question and issues getting it to work.
Issue
Correct me if i'm wrong but the particle system is defined as follows
void Start()
{
ParticleSystem ps = GetComponent<ParticleSystem>();
var main = ps.main;
main.startDelay = 5.0f;
main.startLifetime = 2.0f;
}
Now if I have 3 particle systems, how do I specify which one i'm referring to? Since I cant define it as public and drag the particle system to it in the editor anymore?
Issue B
Now i tried following what unity said in their forums and did the following
ParticleSystem ps = GetComponent<ParticleSystem>();
var main = ps.main;
main.maxParticles = 150;
// LeafStormParticleSystem.maxParticles = 150;
do not create your own module instances, get them from a ParticleSystem instance UnityEngine.ParticleSystem+MainModule.set_maxParticle
Any help with Issues A and B will be appreciated.
Thank you
Issue A
Only one ParticleSystem component can be attached to any one GameObject at a time. Sub-ParticleSystems must therefore be attached to separate GameObjects too (generally children of the GameObject holding the first ParticleSystem), which can be dropped directly onto public fields.
public ParticleSystem LeafStormParticleSystem;
void Start ()
{
if (LeafStormParticleSystem != null)
{
var main = LeafStormParticleSystem.main;
main.maxParticles = 150;
}
}
Issue B
Your code looks fine, however a critical part of the error message was missing; NullReferenceException which is telling you that a reference in your code is equal to NULL. In your case, that reference would be the ps variable used to store the ParticleSystem reference, which is either a consequence of not attaching this script to the GameObject holding your ParticleSystem or simply that you have no ParticleSystem attached at all. In either case, make sure both script and ParticleSystem are attached to the same GameObject and check your references like so;
void Start ()
{
ParticleSystem ps = GetComponent<ParticleSystem>();
if (ps != null)
{
var main = ps.main;
main.maxParticles = 150;
}
}
I am working on a 2D game and have created a game object using C# script as below. I also set my camera to orthogonal and have adjusted my sprite based on the width of the screen. Regardless of the position I set, the object is always at the center of the screen. How can I solve this?
using UnityEngine;
using System.Collections;
public class TestingPositions : MonoBehaviour {
GameObject hero;
Sprite heroSprite;
Vector3 heroPosition;
// Use this for initialization
void Start () {
hero = new GameObject ();
Instantiate (hero, heroPosition, Quaternion.identity);
Camera camera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
heroPosition = camera.ScreenToWorldPoint(new Vector3(Screen.width/4, Screen.height/4, camera.nearClipPlane));
heroSprite = Resources.Load <Sprite> ("Sprites/heroImage");
SpriteRenderer renderer = hero.AddComponent<SpriteRenderer>(); renderer.sprite = heroSprite;
}
}
when you use Instantiate you have to use it on
an existing model.
Instantiate means "duplicate this model" or "copy this model", or "make a new one, using this model as an example".
What you are doing, is creating a brand new empty "hero" game object - and then "instantiating" it. That is meaningless and does nothing.
What you must do whenever you want to use "Instantiate" is this:
public GameObject modelPerson;
Note that the name must be "modelSomething".
first put that in your code. LOOK at the Inspector. MAKE your actual model hero (or whatever it is)
Sit it somewhere off camera where it is not seen.
Now, drag that thing to the "modelPerson" slot in the Inspector.
If you are not familiar with the basics of using Inspector-dragging in Unity, review basic Unity tutorials https://unity3d.com/learn/tutorials/topics/scripting
Next in your code, perhaps in Start, try this
GameObject newHero = Instantiate( modelPerson );
newHero.transform.position = .. whatever you want
newHero.transform.rotation = .. whatever you want
newHero.name = "Dynamically created";
newHero.transform.parent = .. whatever you want
once you understand these basics, there is very much more to learn about Instantiate. You can ask that in separate questions. Good luck.
Your need to save the reference to your gameObject that is created with Instantiate, because Instantiate makes a copy not modifies the original.
To modify a gameobjects position after instantiation, you need to use gameobject.transform.position = newPosition; To modify it before instantiation, you would need to do the "heroPosition" line before using heroPosition in Instantiate.
So like this:
using UnityEngine;
using System.Collections;
public class TestingPositions : MonoBehaviour
{
GameObject hero;
SpriteRenderer heroSprite;
// Use this for initialization
void Start()
{
Camera camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
//Save the reference to the instantiated object into a variable
//Since you are creating an object from scratch, you don't even need Instantiate, which means copy - not create.
hero = new GameObject();
//Set its position
hero.transform.position = camera.ScreenToWorldPoint(new Vector3(Screen.width / 4, Screen.height / 4, camera.nearClipPlane));
//Set its rotation
hero.transform.rotation = Quaternion.identity;
//Add sprite renderer, save the reference
heroSprite = hero.AddComponent<SpriteRenderer>();
//Assign the sprite
heroSprite.sprite = Resources.Load<Sprite>("Sprites/heroImage");
}
}
I'm trying to make a script to set an object when is being instantiated. The problem is, I don't clearly know how to do it. I have this function..
function spawnPlayer()
{
var CameraScript = GameObject.Find(PlayerPrefab.name).GetComponent("Camera Control");
Network.Instantiate(PlayerPrefab, spawnObject.position, Quaternion.identity, 0);
}
Where PlayerPrefab is going to be the Prefab that's going to be instantiated. When this happens, I need to set the instantiated gameObject on another GameObject which is camera and has a script called "Camera Control" and inside there's a transform Target which I'm trying to set. How to do this?
The code you posted can't be right. You are using the PlayerPrefab's name to to find the Camera Control script attached to the camera? By that logic then the moment you instantiate PlayerPrefab, on the second line, you will have a second camera.
I think what you want to do is this: Instantiate the player prefab and make the camera point to the player.
So I am assuming the CameraControl script is created. You need the following before we start to code.
Attach CameraControl script to the camera in the scene.
Make sure the Player script is attached to the Player Prefab.
Have a third script that will instantiate the PlayerPrefab. I will call it Instantiator. Attach it to an empty GameObject in the scene, think of it as the world GameObject. We will call it World.
Make sure the Instantiator script is attached to the World GameObject and that it is pointing to the PlayerPrefab.
Code: Instantiator
The Instantiator script will spawn and create things we will use in the scene.
#pragma strict
var PlayerPrefab : GameObject;
function Start ()
{
// You can add position and rotation to the function call if you like.
var p = Instantiate(PlayerPrefab) as GameObject;
// Find the camera script and point to Player's transform.
Camera.main.GetComponent("CameraControl").SendMessage("setTarget", p.transform);
}
Notice I used the fact that the MainCamera in the scene is marked by Unity for you so it is easy to find.
Code: CameraControl
The CameraControl will have the logic to follow the Player as you see fit. Notice that target will point to what the camera will focus on. Of course following the Player around you will have to write.
var target : Transform;
function setTarget(t : Transform)
{
target = t;
}
I just taught myself a bit of JavaScript. I had never used it before.
I found my solution.
What I was meaning on my question was to set my camera's script the transform of the instantiated object.
I did not have to make many empty objects with scripts value of each object; it took me hours to find it because I did not know how unity handles the scripts objects calls.
This is how I made it:
var PlayerPrefab : GameObject;
var spawnObject : Transform;
private var MainCamera : GameObject;
function spawnPlayer()
{
var player = Instantiate(PlayerPrefab, spawnObject.position, Quaternion.identity);
MainCamera = GameObject.Find("MainCamera");
player.name = "Ball";
if(MainCamera)
{
MainCamera.GetComponent.<CameraControl>().target = player.transform;
Debug.Log("Succeed.");
}
}
Like this, my camera will have the transform properties of the new instantiated object automatically.
I'm working with an android project in Unity 3d.
I would to roll the sphere at the surface of a cube.
However, when I clicked the play button it returns error message:
Assets/Scripts/Player.js(4,1): BCE0005: Unknown identifier: 'rigidBody'.
My code:
function Start () {
rigidBody.velocity.x=15;
}
Rigidbody components has been already added to the sphere.
I would like to seek solution to the error generated.
I don't know if you've set a GetComponent variable on the rigidbody, but you may have to strip the case out of that.
For example:
rigidBody.velocity.x=15;
would be:
rigidbody.velocity.x=15;
Hope that helps.
First, it's "rigidbody" not "rigidBody"
Second, starting with Unity 5 + something you cannot use "rigidbody"
anymore, so you have to use GetComponent
Resuming, to use "rigidBody" as it is, you have to initialize it first like others answered you already:
//link you rigidbody here:
public Rigidbody rigidBody;
function Start() {
//Or if the script is on the GameObject that has the rigidbody component:
//rigidBody = GetComponent<Rigidbody>();
rigidBody.velocity=new Vector2(15,0);
}
I think you forgot to initialize a Rigidbody. Also you cannot assign a velocity like this because rigidBody.velocity.x is a read-only value.This code might help you:
public Rigidbody rigidBody;
function Start(){
rigidBody.velocity=new Vector2(15,0);
}
You haven't initialized the variable "rigidBody".
I don't think that's your objective though.
If you have the script added to the sphere as a component, you don't have to use getComponent. Instead it's going to be just:
"Rigidbody.velocity.x=15;"
You might have to use a "new Vector3(x,y,z);" to pass the new velocity on. In that case the code would look like this:
Rigidbody.velocity = new Vector3(15,Rigidbody.velocity.y, Rigidbody.velocity.z)*
I'm working in 2D right now, so my parameters of Vector3 might be off.
I thought Rigedbody was correct, but it might be rigidbody - see above.
In any case, don't forget your colliders. Ridged bodies don't automatically collide with other objects, but they are subject to gravity. Once I finally figured that out, I just dropped my character 20 feet onto pavement out of joy. Rendering blood is surprisingly easy if you're not that picky.
you need add rigidbody components in inspectors first then :
Rigidbody sphereRigidbody;
function Awake(){
sphereRigidbody = GetComponent<Rigidbody>();
sphereRigidbody.velocity = new Vector3(15,0,0);
}
for c#
You might want to cache it first.
private Rigidbody rigidbodyCached;
//cache
void Start(){
rigidbodyCached = this.GetComponent<Rigidbody>();
}
//for velocity movements use FixedUpdate instead of Update
void FixedUpdate(){
rigidbodyCached.velocity = new Vector3(15,0,0);
}
if you working with unity less than 5 (I guess) you have access to use components of game objects like rigidbody or audiosource but in unity 5 and later you need to add a reference to that in neither in awake or start function like this code
private Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
// AND AFTER YOU ADDED THE REFERENCE FOR RIGIDBODY
// THEN CHANGE THE VELOCITY LIKE THIS
rb.velocity.x = 20;
}